Don't post the same question multiple times on this forum, once is enough. Also, please use code tags.

Finally tags are always executed, regardless of if an exception was caught/thrown/propagated. In your above example there's no difference because the second example is guaranteed to always be executed (because you are catching the only possible exception that could be thrown).

However, consider a case where multiple exceptions could be thrown, or if your try block throws an exception:

Dear friend, Finally block is always executed. In the above example (1st) if you do not use catch block then the exception is occur and this exception is handled by JVM, and then JVM terminate the program, but before terminate the program the finally block must be executed.
And in the above example(2nd), If you do not use catch block then the exception is occur and your program will be terminated by the JVM. It means any statements are not be executed.

July 25th, 2013, 09:06 AM

qazi

Re: Finally Block

Yes finally block will always be executed except when you have system.exit() in try or catch ..

October 29th, 2013, 03:03 AM

mk12345

Re: Finally Block

it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.