Call return call or System.exit in a try or catch block

I had the following question asked in an interview:

What happens if you call the return statement or System.exit when you try or the catch block? Will the block complete execution?

Is the final block being executed?

EDIT: After reading this in java:

  • finally block is executed if I put a return statement in a try or catch block, but

  • finally block does not start if I call the System.exit try or catch form.

I do not understand the reason.

+6
source share
3 answers

What happens if you call the return statement or System.exit when you try or the catch block?

Will the block complete execution?

In the case of a return Yes. If you need gory details, they are listed in JLS 14.20.2 .

(Note that in JLS terminology, a return is considered a sharp termination, but it doesn’t matter, because if you carefully analyze the specification, you will see that finally is executed for both normal and abrupt endings.)

In the case of System.exit() , no. A call to the exit method never returns, and it does not throw an exception either. Therefore, finally closing clauses for a thread are never executed.

(In JLS, the call to exit() does not β€œend at all." It is conceptually the same as a method that goes into an infinite loop that (magically) uses no processor time. Associated with disabling the JVM, occurs on other threads.)

+6
source

According to tutorials from Oracle :

Note. If the JVM terminates while try or catch code is running, then the finally block may not be executed. Similarly, if the thread that is attempting or the catch code is interrupted or killed, the finally block may not be executed even if the application as a whole continues.

This statement means that:

  • The finally block will not execute if System.exit(0) is called System.exit(0) since the Java VM exits when this statement is called )
  • it will be executed when the return called ( since the Java virtual machine does not exit when this statement is called ).

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

 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 the terminal, but when System.exit(0) is called in the catch , it is not.

+3
source

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

Here is JLS or it points out:

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

for catch block

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

Since you call System.exit , which terminates the program in a try block , then try-catch is not executed normally, so the finally block will not be executed.

For the return statement try-catch completes normally because you are not blocking the try block (for example, an infinite loop, a sleeping thread, terminating the program, etc.), so the finally block is executed after you return from the method.

+2
source

Source: https://habr.com/ru/post/973917/


All Articles