Hi all, look at the following 2 examples... Example 1
What is the result of attempting to compile and run the program? a. Prints: 0,1,0,0 b. Prints: 1,1,0,1 c. Prints: 0,1,0,1 d. Prints: 0,1,1,1 e. Prints: 1,1,1,1 f. Compile-time error g. Run-time error h. None of the above Answer is --- f Compile-time error A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block.
What is the result of attempting to compile and run the program? a. Prints: 0,1,0,0 b. Prints: 1,1,0,0 c. Prints: 0,1,1,0 d. Prints: 1,1,1,0 e. Prints: 1,1,1,1 f. Compile-time error g. Run-time error h. None of the above Answer is --- c Prints: 0,1,1,0 Why does the compiler not complain about the WhiteException that is not thrown as it complains in example 1 for BlueException?? Am i missing something ?? thanks, [ Jess adjusted where the [code] tags end so that word wrapping doesn't get all screwed up with the text ] [ January 31, 2004: Message edited by: Jessica Sant ]
"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Hi Vishy, In the 1st example, since there is no inheriance relationship between RedException and WhiteException and m1() does NOT throw BlueException, thus, if you catch the BlueException in a try-catch-block, the compile sure complain that, BlueException is never thrown. In the 2nd example, on the other hand, there DOES inheriance relationship beteweeh ColorException and WhiteException. The relationship is WhiteException extends ColorException. In method m1(), it declares to throw ColorException. According to the Java rule, if a method that declare to throw Exception E, if E (or exception classes that extend E [subclasses of E]) is encountered, it will be thrown without any problems, and thus, the try-catch-block of m1() has no problem. Does this make sense? Nick.
Hello: i think the answer should be "complie-time error" since m2 just declares to throw a whiteException and never throws it in the method body. Can anyone explain if i am missing something here??? Thanx -S
Remember that the method has declared that it might throw the exception, this is enough for the compiler. either use try/catch or declare it. the throw keyword makes the exception happen, whereas throws says it might throw, but it might not. hope this helps. davy