7

I got the following question asked in interview :

What will happen if one calls a return statement or System.exit on try or catch block ? Will finally block execute?

Does the finally block always get executed?

EDIT: After trying the above in java:

  1. finally block executes if I put return statement in try block or catch block but

  2. finally block doesn't run if I call System.exit form try or catch.

I don't get the reason behind though.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142

3 Answers3

7

What will happen if one calls a return statement or System.exit on try or catch block ?

Will finally block execute?

In the case of a return, Yes. If you want the gory details, they are specified in JLS section 14.20.2.

(Note that in the JLS terminology, a return counts as an abrupt termination. But that doesn't matter, because when you analyse the spec carefully, you will see that the finally gets executed for both normal and abrupt terminations.)

In the case of a System.exit(), No. The call to the exit method never returns, and it doesn't throw an exception either. Therefore the "enclosing" finally clauses for the thread never get executed.

(In JLS parlance, the exit() call doesn't "terminate" at all. It conceptually the same as a method going into an infinite loop that (magically) doesn't use any CPU time. All of the activity associated with JVM shutdown happens on other threads.)

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

According to the tutorials from Oracle:

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.

This statement seems to imply that:

  • The finally block will not execute if System.exit(0) is called (since the Java VM exits when that statement is called)
  • it will execute when a return statement is called (since the Java VM does not exit when that statement is called).

You can confirm this with some code I quickly wrote up:

public class TryExample {
    public static void main(String[] args)
    {
        try {
            int[] i = {1, 2, 3};
            int x = i[3];//Change to 2 to see "return" result
            return;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("caught");
            System.exit(0);
        } finally {
            System.out.println("finally");
        }
    }
}

Here, when return is called in the try block, "finally" is still output to terminal, but when System.exit(0) is called in the catch block, it is not.

Nick Meyer
  • 1,771
  • 1
  • 17
  • 29
2

No it wont, when the try-catch does not complete normally then finally block wont get executed.

Here is the JLS or it and states:

If execution of the try block completes normally, then the finally block is executed

same for catch block

 If the catch block completes normally, then the finally block is executed.

Since you are calling System.exit which terminate the program within the try block then try-catch does not complete normally thus finally block wont get executed.

For the return statement try-catch does complete normally because you are not blocking the try block(such as an infinite loop, sleeping the thread, terminating the program, etc.) thus finally block is executed after you return from the method.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63