Try-catch-finally in java

Nikhil picture Nikhil · Aug 22, 2011 · Viewed 52.6k times · Source

In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ?

Answer

Gabriel Negut picture Gabriel Negut · Aug 22, 2011

The only time a finally block will not be executed is when you call exit() before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run.

EDIT: This is not entirely correct. See the comments below for additional information.