0

So I've been assigned to teach a block on exception handling, and I've run into a question I don't have an answer for. What valid (e.g., don't result in either a compiler, or a run-time, error) commands in Java will keep a finally-block from executing?

A return statement won't do it, but a System.exit(0) statement will. My understanding is that a finally-block would execute no matter what. What (valid) statements will prevent a finally-block from doing so, and why?

Chance
  • 988
  • 2
  • 13
  • 29
  • The purpose of `finally` is to execute something no matter what ( in terms of try-catch scenario). So selective bypassing `finally` is using a mechanism to achieve something which is not meant for doing that thing. I guess then there is some problem in logic flow / design. Do you have some use case where such a thing is necessary or is it a generic question? Using `exit` then sounds valid answer if program continuation is not expected. –  Oct 12 '15 at 22:07
  • `Machine.emergencyPowerOff()` – ZhongYu Oct 12 '15 at 22:28

3 Answers3

2

Quoting Docs:

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.

Regarding your example about System.exit(), quoting Docs again

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

which means that finally will not be executed.

Simple example:

try {
    System.out.println("try");
    System.exit(0);
} finally {
    System.out.println("finally");
}

The above example will print only try but not finally

sam
  • 2,033
  • 2
  • 10
  • 13
1

To quote Oracle's tutorials:

Note: 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.

System.exit will just quite the JVM, thus preventing the finally block from running, as will killing the thread containing that block from another thread. Another edge case is having an infinite loop in the try block, which will simply prevent it from ending, so the finally loop will never be reached.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

I see three cases to never reach finally block (or any further instructions in the stack frame):

  1. Sudden thread death (including JVM exit)
  2. stack frame operations
  3. Infinite loop ; whatever it's active (while (true)) or passive (acquiring lock, waiting for notification, etc.)

Only native code (including own JVM mechanisms) is supposed to kill thread or play with stack frames. Fatal system errors are also an option.

However, JVM implementations are free to offer such (undesirable ?) feature across his specific API (ie sun.misc.Unsafe) or via an ugly implementation of Java SE API (ie Thread.destroy)

LoganMzz
  • 1,597
  • 3
  • 18
  • 31