Amazon Interview Question
Software Engineer / Developersfinally block will execute EVERYTIME EXCEPT.... 1)before finally Block, JVM Shutdowns the existing Program anyhow 2) if there is unhandled exception in finally block. Eg:-System.exit(); .... simple as that
Even a return statement in any try - catch block will not stop the execution of finally block. (Tried and Tested :0 )
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — 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.
- deaGle September 27, 2008