How finally works. Fragment
try { throw new Exception(); } catch(Exception e) { throw new Exception(); }
will end abruptly, but the finally clause takes effect, and when it returns, it cancels the original reason for the completion of the statement.
This is explained in the Blocks and Expressions section of the Java Language Specification. I highlighted the appropriate path in your situation:
A try with The finally block is executed by first executing a try . Then there is a choice:
- If the execution of the
try block completes normally, then the finally block is executed, and then there is a choice: - If the execution of the
try block suddenly terminates due to the throw value V , then there is a choice:- If the run-time type V is equal to that assigned to the parameter of any
catch try , then the first (leftmost) one is catch . A catch clause is catch . The value V is assigned to the parameter of the selected catch , and the block of this is met. The catch condition is met. Then there is a choice:- If the
catch completes normally, the finally block is executed. Then there is a choice: - If the
catch terminates abruptly due to R , then the finally block is executed. Then there is a choice:- If
finally completes completion normally, try automatically terminates Cause R. - If the
finally block terminates abruptly for reason S , then try automatically ends reason S (and reason R is discarded).
- If the run-time type of V is not assigned to any
catch try clause, then the finally block is executed. Then there is a choice:
- If the execution of the
try block terminates abruptly for any other reason R , then the finally block is executed. Then there is a choice:
source share