Pritish Chakraborty wrote:Follow the flow control, Kedar!
You throw an exception in the try block, which is silently caught by the catch block.
The catch block then rethrows the exception. Now either it will move up the call stack (out of main()), or it will be caught again.
But in the last fragment of code, a finally block is placed. A finally block runs regardless of whether an exception is thrown. In this case the exception *is* rethrown from catch, and is 'caught' by finally, which forcefully executes. I always considered finally as an offshoot of the catch(...) clause in C++ but let's not go there, finally has some fundamental differences.
Now if we rethrow from finally, the exception is thrown out of main() and is caught by the JVM, which prints the stack trace after halting the program execution.
Now try this - don't throw a new exception from finally. Post the result.
Understood. That cleared my doubt!!
Thanks!