10

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
GuruKulki
  • 25,776
  • 50
  • 140
  • 201

9 Answers9

25

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

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
6

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

msw
  • 42,753
  • 9
  • 87
  • 112
3

In the Java documentation:

http://java.sun.com/docs/books/tutorial/essential/exceptions/finally.html

It explains Finally very well.

They do note that if the JVM exits, that the finally block will not be called. Or if a thread that is running the block of code gets killed, the finally block will not be called. In all other cases it will.

MarkPowell
  • 16,482
  • 7
  • 61
  • 77
3

One thing I can think of right now is an OutOfMemoryError in which case there is a chance that no further code in your app can be executed.

alex.zherdev
  • 23,914
  • 8
  • 62
  • 56
  • Even in case of an OutOfMemoryError, the finally block will be executed. The execution of the finally block itself may then of course cause an OutOfMemoryError itself, but that may also happen, even if the try block completed succesfully. – jarnbjo Mar 10 '10 at 15:56
3
try {
    System.out.println("BEFORE");
    System.exit(0);
    System.out.println("AFTER");
} finally {
    System.out.println("FINALLY");
}

this will give you the output:

BEFORE
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
setzamora
  • 3,560
  • 6
  • 34
  • 48
1

System.exit(1); you can use

giri
  • 26,773
  • 63
  • 143
  • 176
1

Another situation that I can think of (that is left out from the other answers) is when an exception is thrown inside a finally block, in that case the finally block will not be "completely" executed.

Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
0

If some Java Native Interface method segfaults (a library function outside of java but called from there crashes) a finally method will also not be called because the entire JVM stops.

Errors in the JVM itself also result in a crash and prevent everything from continued execution.

extraneon
  • 23,575
  • 2
  • 47
  • 51
0

the finally clause in the try-catch exception block always executes, irrespective of the occurrence of exception in the normal java program flow. If the execution flow is stopped before the finally clause then the finally block will not be executed.

we can use System.exit(1); before finally block and stop the execution flow of the program.

Gilles
  • 9,269
  • 4
  • 34
  • 53