Try / finally without catch with return statement?

Why is the result of the following code equal to 3, why, finally, you need to exit and exit the method, even if the compiler checks first and why returning to try does not complete the method?

public int returnVal(){ try{ return 2; } finally{ return 3; } } 
+6
source share
5 answers

See JLS 14.17

You can see that the return statement always terminates abruptly.

The previous descriptions say “attempts to transfer control”, and not just “transfer control”, because if there are any attempts in the method or constructor (§14.20) in which the try or catch method contains a return statement, then, finally, the sentences these try statements will be executed to be most internal to external, before control is passed to a method or constructor call. Abrupt completion of the finally clause can lead to a violation of the control transfer initiated by the return statement.

Essentially check the phrases attempts to transfer control and the last sentence. A return attempt attempts to transfer a control, and then disrupt the transfer of control initiated by a return statement .

In other words, try attempts to transfer the controll , but since the execution of the finally block is still open for execution and contains the return statement, the attempt to transfer the control in the finally block has higher preceding . This is why you see the value 3 , which is returned inside the finally block.

+4
source

finally is executed before returning the value from try (or catch ).

If the finally a return present, it overwrites the return of the try (or catch ) catch .

So in this case, the return finally wins:

 try { // Exception thrown return 2; } catch (Exception e) { return 1; } finally { return -1; // Always returns -1 also if a return statement is present in the try and in the catch clause } 
+4
source

Why returning to try does not end the method?

Is it really bad practice to return from a finally catch try block?

You should know that when you put a return inside a try-catch statement, the return itself is held and controlled by the exeption processing unit.

The exception block only records your return, and it really returns it ONLY after all the checks have been completed, which does not mean that the finally block will really affect your return:

 public int returnVal() { int i = 0; try { return i; } finally { i = 99; } } 

In this case, the return will be 0.

But in the case that you mentioned in your question, you have an impact on the return a second time, and since the exception block does not want your return to pass until it is completed with all its validation, you always save the return value 2 times to your exception block. The result will always be 3.

Just keep in mind that if you put the answer inside try-catch-finally, this return will not return until the last block completes; the return itself is only saved until everything is verified.

+1
source

final explication , when you write a finnaly block, they always execute it.

0
source

The finally block is always executed before control returns to the calling function. Thus, in this case, before returning 2, he will call finally and return 3.

0
source

All Articles