10

Possible Duplicate:
Is there such case when in try\finally block the finally won't be executed?

is there any code, that will never execute finally clause?

Cœur
  • 37,241
  • 25
  • 195
  • 267
developer
  • 9,116
  • 29
  • 91
  • 150

7 Answers7

7

Java Tutorials

Not only for System.exit , but also for thread interrupted

Note: If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.

Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • 2
    Can you clarify the point about interruption? `Thread.interrupt()` can't make thread to bypass `finally` block. – axtavt Apr 07 '11 at 11:47
  • The point about killing a thread is also not clear - `Thread.stop()` causes an exception to be thrown, so that `finally` block is triggered as usual. – axtavt Apr 07 '11 at 11:54
  • 1
    @axtavt: the OS can kill the thread, and the owner application will not be notified. I have seen this behavior for on app I was developing. – khachik Apr 07 '11 at 11:56
4

System.exit(0) is one example. If you compile and run below "Bye" will never get printed.

public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Hi");
            System.exit(0);
        }
        finally {
            System.out.println("Bye!");
        }
    }
}
Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
4

System.exit(0) if does not throw security exception.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
4

Here's another example:

try {
    while (true) {
       System.err.println("Its great to be alive");
    }
} finally {
    System.err.println("I wish!");
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    @Suresh S: Well, it *technically* does what you asked for ;o) – Piskvor left the building Apr 07 '11 at 12:33
  • This code can be interrupted so it should go to finally block, shouldn't it? – Puterdo Borato Jun 23 '12 at 12:17
  • @PuterdoBorato - The code won't notice any interrupts. – Stephen C Jun 23 '12 at 16:53
  • @StephenC, if we execute the mentioned code in thread and then call interrupt() on the thread? Why wouldn't it notice interrupts? – Puterdo Borato Jun 24 '12 at 09:40
  • 1
    Because the only way to notice an interrupt is to check for it. Most processes which block (using a lock, various IO streams, etc) will check the interrupt status and throw an exception so they do not run if the thread is supposed to be interrupted. If you don't check for it, or use any code that checks for it, thread.interrupt() does nothing but set the interrupted flag. – Ajax Jul 05 '16 at 23:34
2
System.exit(0);
jmj
  • 237,923
  • 42
  • 401
  • 438
1

In short words, finally block is not executed if JVM is stopped while thread is inside a corresponding try block, or if thread is killed using some low-level mechanisms (not Thread.stop()).

In addition to obvious examples with System.exit(), even a normal JVM shutdown can be unexpected for daemon threads:

public static void main(String[] args) throws Exception {
    final CountDownLatch c = new CountDownLatch(1);

    Thread t = new Thread() {
        public void run() {
            try {
                System.out.println("Entering try block");
                c.countDown();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {}
            } finally {
                System.out.println("Never printed");
            }
        }
    };
    t.setDaemon(true);
    t.start();
    c.await();
    System.out.println("Exiting main thread");
}
axtavt
  • 239,438
  • 41
  • 511
  • 482
0

Also on my workout i found a new solution for this, is this can be accepcable

try {
if (choice) {
  while (true) ;
} else {
  System.exit(1);
}
} finally {
code.to.cleanup();

}

developer
  • 9,116
  • 29
  • 91
  • 150