Pages Navigation Menu

Coding is much easier than you think

What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?

What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?

 
Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions

  • If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to System.exit() call.
  • if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
  • If a exception is thrown in finally block and not handled then remaining code in finally block may not be executed.


About Mohaideen Jamil