Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there possibility that a finally block might not execute? [duplicate]

Tags:

java

exception

We know that no matter whether an exception is thrown, or caught and handled it, the finally block will get executed, So I was curious that is there any possibility that finally block will not executed.

And if System.exit() is called either in try or catch, then also will the finally gets called?

like image 486
GuruKulki Avatar asked Mar 10 '10 15:03

GuruKulki


2 Answers

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, 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.

Source: java.sun.com: Java Tutorial: The finally Block

like image 155
Daniel Vassallo Avatar answered Oct 11 '22 04:10

Daniel Vassallo


System.exit() will prevent a finally block from executing.

like image 28
msw Avatar answered Oct 11 '22 03:10

msw