A try statement with a finally block is executed by first executing the try block. Then there is a choice:
-
If execution of the
tryblock completes normally, then thefinallyblock is executed, and then there is a choice:- If the
finallyblock completes normally, then thetrystatement completes normally. - If the
finallyblock completes abruptly for reasonS, then thetrystatement completes abruptly for reasonS.
- If the
-
If execution of the
tryblock completes abruptly because of athrowof a valueV, then there is a choice:-
If the run-time type of
Vis assignment compatible with a catchable exception class of anycatchclause of thetrystatement, then the first (leftmost) suchcatchclause is selected. The valueVis assigned to the parameter of the selectedcatchclause, and the Block of thatcatchclause is executed. Then there is a choice:-
If the
catchblock completes normally, then thefinallyblock is executed. Then there is a choice:- If the
finallyblock completes normally, then thetrystatement completes normally. - If the
finallyblock completes abruptly for any reason, then thetrystatement completes abruptly for the same reason.
- If the
-
If the
catchblock completes abruptly for reasonR, then thefinallyblock is executed. Then there is a choice:- If the
finallyblock completes normally, then thetrystatement completes abruptly for reasonR. - If the
finallyblock completes abruptly for reasonS, then thetrystatement completes abruptly for reasonS(and reasonRis discarded).
- If the
-
-
If the run-time type of
Vis not assignment compatible with a catchable exception class of anycatchclause of thetrystatement, then thefinallyblock is executed. Then there is a choice:- If the
finallyblock completes normally, then thetrystatement completes abruptly because of athrowof the valueV. - If the
finallyblock completes abruptly for reasonS, then thetrystatement completes abruptly for reasonS(and thethrowof valueVis discarded and forgotten).
- If the
-
-
If execution of the
tryblock completes abruptly for any other reasonR, then thefinallyblock is executed, and then there is a choice:- If the
finallyblock completes normally, then thetrystatement completes abruptly for reasonR. - If the
finallyblock completes abruptly for reasonS, then thetrystatement completes abruptly for reasonS(and reasonRis discarded).
- If the
Example 14.20.2-1. Handling An Uncaught Exception With finally
class BlewIt extends Exception {
BlewIt() { }
BlewIt(String s) { super(s); }
}
class Test {
static void blowUp() throws BlewIt {
throw new NullPointerException();
}
public static void main(String[] args) {
try {
blowUp();
} catch (BlewIt b) {
System.out.println("Caught BlewIt");
} finally {
System.out.println("Uncaught Exception");
}
}
}
This program produces the output:
Uncaught Exception
Exception in thread "main" java.lang.NullPointerException
at Test.blowUp(Test.java:7)
at Test.main(Test.java:11)
The NullPointerException (which is a kind of RuntimeException) that is thrown by method blowUp is not caught by the try statement in main, because a NullPointerException is not assignable to a variable of type BlewIt. This causes the finally clause to execute, after which the thread executing main, which is the only thread of the test program, terminates because of an uncaught exception, which typically results in printing the exception name and a simple backtrace. However, a backtrace is not required by this specification.
The problem with mandating a backtrace is that an exception can be created at one point in the program and thrown at a later one. It is prohibitively expensive to store a stack trace in an exception unless it is actually thrown (in which case the trace may be generated while unwinding the stack). Hence we do not mandate a back trace in every exception.