...
Here, the finally block completes abruptly since a return statement occurs within it. As a result, when the exception is thrown in method someException, it does not show up in the output. This is due to the abrupt termination of the finally block that suppresses any useful exception information displayed in the try block by overriding it with its own message. Note that even if the try block returns some value, the finally block is executed.
Note that MullPointerException is a RuntimeException, and thus does not need to occur in a throws declaration. The throwException() function never actually throws a SomeException class, although it claims to via its throws declaration.
| Code Block | ||
|---|---|---|
| ||
class SomeException extends Exception {
public SomeException(String s) {
super(s);
}
}
class TryFinally {
private static void throwException() throws SomeException {
throw new NullPointerException();
}
static private boolean doLogic() {
try {
throwException();
} catch (SomeException se) { System.out.println("Exception thrown"); }
finally {
System.out.println("Uncaught Exception");
return true;
}
}
public static void main(String[] args) {
doLogic();
}
}
|
...
| Code Block | ||
|---|---|---|
| ||
class SomeException extends Exception {
public SomeException(String s) {
super(s);
}
}
class TryFinally {
private static void throwException() throws SomeException {
throw new NullPointerException();
}
static private void doLogic() {
try {
throwException();
} catch (SomeException se) { System.out.println("Exception thrown"); }
finally {
System.out.println("Uncaught Exception");
}
return true;
}
public static void main(String[] args) {
doLogic();
}
}
|
...