Example & Tutorial understanding programming in easy ways.

When a finally block does not get executed?


The reason for this behavior is, call to system.exit() is processed in a runtime system thread which may take time to shutdown the jvm, meanwhile thread scheduler can ask finally to execute. So finally is designed to always execute, but if you are shutting down jvm, it may happen that jvm shuts down prior to finally getting being executed.

try { System.out.println("hello"); System.exit(0); } finally { System.out.println("bye"); } // try-finally

"bye" does not print out in above code.

Read More →