7

when does the finally block not execute while try or catch block is interrupted? the doc says that "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." can someone give an example about this situation?

andy
  • 3,951
  • 9
  • 29
  • 40
  • So what's the question? – Caveman42 Jul 02 '13 at 11:49
  • @RAS yes, and in [Does finally always execute in Java?](http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java), i find what i want to know.But, what i mean is not identical to that question. However, the answer does exist there. – andy Jul 02 '13 at 13:34

4 Answers4

11

Good answers can be found here.

Besides a System.exit(), the finally block will not run if the JVM crashes for some reason (e.g. infinite loop in your try block).

As far as the thread itself, only if it is stopped using the stop() method (or suspend() without resume()) will the finally block not be executed. A call to interrupt() will still result in the finally block being executed.

It's also worth noting that, since any return statement in the finally block will override any returns or exception throws in the try/catch blocks, program behavior can quickly become erratic and hard to debug if this happens to you. Not really anything worth taking precaution against (as it only happens in extremely rare cases), but worth being aware of so you can recognize it when it happens.

Community
  • 1
  • 1
Pat Lillis
  • 1,152
  • 8
  • 19
  • thanks for your answering, but what i really want to find is the meaning of interrupted in the sentence **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.**. Does it means stop for **stop()** or **suspend()**. As in your answer, A call to **interrupt()** does not stop the **finally** from being executed. – andy Jul 02 '13 at 13:26
  • Is [this](http://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do) what you're looking for? – Pat Lillis Jul 02 '13 at 13:29
  • 1
    This is wrong. If a thread is stopped via `Thread.stop`, `finally` blocks *are* executed. – Holger Nov 12 '14 at 15:20
7

The only legal way to make finally not execute is to call System.exit or Runtime.halt in try or catch

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Abruptly ending the application before it, e.g.

System.exit();

Even the return before finally block (e.g. in try block) will be executed after the finally.

darijan
  • 9,725
  • 25
  • 38
1

This refers to things that are (mostly) outside of the envelope of normal Java execution.

  • If your thread (successfully) calls System.exit() in the try block or a catch block, that call won't "terminate" in the sense used in the JLS. Same thing happens if another thread calls System.exit(). In this case the JVM just runs the shutdown hooks.

  • If the JVM (hard) crashes, all Java activity ceases instantly.

  • If the OS (hard) kills the JVM process, all Java activity ceases instantly.

  • If the power to the CPU chip goes off...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216