Most of the existing answers contain pieces of the right answer, but none are quite spot-on.
The finally
block is always guaranteed to be reached after the try
and potentially catch
blocks if the JVM does not shut down beforehand. However, if a bit of code inside the finally
block shuts down the JVM, or throws an exception its own, the end of the block might not be reached.
Per the Sun Certified Programmer for Java 6 Study Guide:
The final word is, as always, the Java Language Specification. The behavior of finally
is explained exhaustively in §14.20.2 Execution of try-catch-finally.
As an extra note: you're right that a return
in the try
won't stop finally
from running. In fact, finally
is entered immediately after the return
is encountered, before it executes.