Usually, finally will be called after the execution of the try or catch code blocks.
The only times finally won’t be called are:
- If you invoke
System.exit() - If the JVM crashes first
- If the JVM reaches an infinite loop (or some other non-interruptable, non-terminating statement) in the
tryorcatchblock - If the OS forcibly terminates the JVM process; e.g.,
kill -9 <pid>on UNIX - If the host system dies; e.g., power failure, hardware error, OS panic, et cetera
- If the
finallyblock is going to be executed by a daemon thread and all other non-daemon threads exit beforefinallyis called