From what I have understood if a risky method in a try block throws an exception, the rest of the try block is not executed. Instead, the catch/finally blocks are executed. But what about the rest of the code in the same method as the try block.
The output of the above code is 1 4 5, which implies that the code after "finally" is executed.
The output is 13423, which implies that the code after finally is not executed.
A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not.
Even if there is a return statement in the try block, the finally block executes right after the return statement is encountered, and before the return executes!
Based on the code sample provided, I think the behaviour is right.
Hit if(i==2) -> throw exception, handle exception print "2" and return, but just before returning, execute finally -> print 3.
Hope this clarifies.
Joined: Oct 15, 2010
I completely missed the return statement! Makes sense now, thank you!