Multiple returns: which one sets the final return value?

Given this code:

String test() { try { return "1"; } finally { return "2"; } } 

Does the language value determine the return value of the test() call? In other words: is it always the same thing in every JVM?

In the Sun JVM, the return value is 2 , but I want to make sure that it is not VM dependent.

+37
java return finally
Feb 22 '10 at 9:35
source share
6 answers

Yes, the language specification defines that the result is "2". If the VM does it differently, it is out of specification.

Most compilers will complain about this. For example, Eclipse will state that the returned block will never be executed, but this is incorrect.

It's a terribly bad practice to write code like this, never do this :)

+40
Feb 22 '10 at 9:38
source share

Yes, the Java language specification is very clear on this issue ( 14.20.2 ):

The try statement with the finally block is executed by the first execution of the try block. Then there is a choice:

  • If the execution of the try block completes normally, [...]
  • If the execution of the try block terminates abruptly due to the throwing of the value of V, [...]
  • If the execution of the try block terminates abruptly for any other reason R, then the finally block is executed. Then there is a choice:
    • If the final block completes normally, [...]
    • If the finally block completes abruptly for mind S, then the try statement terminates abruptly for reason S (and reason R is discarded).
+17
Feb 22 '10 at 10:25
source share

The finally block will always be executed, with the exception of the following example:

 String test() { try { System.exit(0); } finally { return "2"; } } 

In this case, the JVM will stop without executing the finally block.

So in your example, the return value will be 2 .

+13
Feb 22 2018-10-22T00 00Z
source share

Yes, if you return something from the finally block, it will replace everything that you could return from the try or catch .

The same is true for exceptions. If you throw something into the finally block, this exception will replace any exception that was thrown in the try or catch . Therefore, be careful not to ever throw something into the finally block, because this may obscure the original cause of the failure.

+7
Feb 22 '10 at 9:52
source share

After reading the byte code of the program, the code looks like this:

The statements of the finally block are queued before the return statement of the try block, so the return from the finally block is done first, and the original return statement never does.

For the program:

 String test() { try { System.out.println("try"); return "1"; } finally { System.out.println("finally"); return "2"; } } 

It is converted to:

 String test() { System.out.println("try"); String s = "1"; //temporary variable System.out.println("finally"); return "2"; Exception exception; exception; System.out.println("finally"); return "2"; } 

And For the program: with a catch block:

 String test() { try { System.out.println("try"); return "1"; } catch (RuntimeException e) { System.out.println("catch"); return "2"; } finally { System.out.println("finally"); return "3"; } } 

Converts to:

 String test() { System.out.println("try"); String s = "1"; System.out.println("finally"); return "3"; RuntimeException e; e; System.out.println("catch"); String s1 = "2"; System.out.println("finally"); return "3"; Exception exception; exception; System.out.println("finally"); return "3"; } 

Note. Done using JDK 1.7 and decompiled using Cavaj.

+2
Jul 28 '15 at 10:27
source share

You can link to the link below. Hope it provides all the details:

http://www.programmerinterview.com/index.php/java-questions/will-finally-run-after-return/

He says that the finally block will always be executed, even a try or catch block has a return statement. And if there is also a return statement at the end of the block, this will override the return statement that is inside the try or catch block, in which case any exception thrown in try / catch will be thrown (bad approach).

Thanks Chethan

0
Mar 26 '15 at 14:25
source share



All Articles