1

This doesn't makes any sense still i am curious to know if it is possible to stop execution of finally block in java? If yes, how? Say for example the below code:

public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Yes, I executed successfully." ); 
        } 
    } 
}

I want the output on the console will not as Yes, I executed successfully.

Well that seems to be possible I concluded. All answers have something different to accept that it is possible. So, I am not accepting any answer specifically. Refer them to reach to conclusion as according to your satisfaction. Thanks to all who posted answers.

Choxx
  • 945
  • 1
  • 24
  • 46

5 Answers5

2
1. A infinite loop above it will stop its execution
2. System.exit() above it will stop it
pola sai ram
  • 832
  • 7
  • 23
  • Yeah, pretty different case you give above, ** A infinite loop above it will stop its execution**. Awesome.. – Choxx Mar 29 '15 at 10:43
  • 1
    An infinite loop definitely doesn't stop the execution of the `finally` block. The block just goes on executing indefinitely long. More importantly, no code following `finally` ever gets executed. – Marko Topolnik Mar 29 '15 at 10:50
2

I haven't come across situation where you practically need. But Its typical interview question and answer is

   System.exit(0) 

See Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
2

Thread.stop() causes an exception to be thrown in the target thread. This happens asynchronously and may just as well happen while executing a finally block. In that case the finally block will complete abruptly, but the thread where it happened is allowed to continue execution.

try {
  try {
    ...code...
  } finally { 
     ...resource cleanup, ThreadDeath thrown here...
  }
} catch (ThreadDeath t) {
  System.out.println("Thread was ordered to stop, ignoring.");
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

I think this says it best:

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.

However, the return statement in the try will not prevent the finally block from executing. Nor would throwing an exception. This behavior is in why the finally block exists in the first place. It lets you reliably clean up resources.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • you mean to say that there are chances in an application that execution continues even if the thread executing the try or catch code is interrupted or killed, and the finally block may not execute? – Choxx Mar 29 '15 at 10:42
  • In a multi-threaded application yes. Though "chance" is misleading. Something would have to deliberately kill the thread. – candied_orange Mar 29 '15 at 10:47
  • well thanks for your answer, but if possible can you give me a link or example for showing that case i said in above comment? – Choxx Mar 29 '15 at 10:56
1

From the JLS §14.20.2

A try statement with a finally block is executed by first executing the try block. Then there is a choice:

  • If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement completes normally.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.
  • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
    • If the run-time type of V is assignment compatible with a catchable exception class of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice:
      • If the catch block completes normally, then the finally block is executed. Then there is a choice:
        • If the finally block completes normally, then the try statement completes normally.
        • If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason.
      • If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:
        • If the finally block completes normally, then the try statement completes abruptly for reason R.
        • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
    • If the run-time type of V is not assignment compatible with a catchable exception class of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
      • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
      • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).
  • If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement completes abruptly for reason R.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

In short, if the JVM does not exit (via System.exit) then the finally block is always run.

But the finally block may exit abnormally, for example by throwing an exception, in this case the finally block will only be partly executed.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • It is *run*, but may not *complete normally*. – Marko Topolnik Mar 29 '15 at 10:47
  • And if there is a case such that death of thread occurs, then in that case will finally block execute? – Choxx Mar 29 '15 at 10:47
  • `ThreadDeath` is the name of the exception thrown by the thread which was ordered to stop. – Marko Topolnik Mar 29 '15 at 10:49
  • Means if a thread stopped using **ThreadDeath **, still the finally will execute? – Choxx Mar 29 '15 at 10:51
  • 1
    @choxx [`Thread.stop`](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()) throws an exception - `finally` would still run. [`Thread.destroy`](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()) - I'm actually not sure, but I presume that the `finally` won't run. Both of these methods are long deprecated however. – Boris the Spider Mar 29 '15 at 10:51
  • 1
    `ThreadDeath` occurring *within* `finally` will curtail its execution and cause it to complete abprutly. This is the concern. – Marko Topolnik Mar 29 '15 at 10:52
  • 1
    @MarkoTopolnik but that's not really a `finally` block problem, but the core reason that these methods were deprecated - the `Thread`drops all monitors and just...goes away. Not as bad as `destory` however... – Boris the Spider Mar 29 '15 at 10:56