Understanding try catch finally with return and value that it returns

怎甘沉沦 提交于 2019-11-29 03:22:10

问题


I have the following piece of code.

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
        return "Return finally value";
    }
}

The output for this is

Executing try
Executing finally
Return finally value

If I change my finally block to not return anything like

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
    }
}

Then the output is

Executing try
Executing finally
Return try value

Now I understand that finally is always executed except if we call system.exit(0); called or the JVM crashes. What I'm not able to understand is why the return value has changed ? I would still expect it to return the value of the try block.
Can anyone explain why the finally value is take into consideration and not the return value from the try block ?

Please refrain from answering because finally is executed even if there is an return in try block ... or finally doesn't execute only if there is a system.exit(0); called or the JVM crashes. as I know that.

EDIT :

(As per Dirk comment on this)
public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return printString("Return try value");
    } catch (Exception e){
        System.out.println("Executing Catch");
        return printString("Return catch value");
    } finally {
        System.out.println("Executing finally");
        return printString("Return finally value");
    }
}

private static String printString(String str){
    System.out.println(str);
    return str;
}

Output:

Executing try
Return try value
Executing finally
Return finally value
Return finally value

回答1:


Just before returning from the main block, the JVM has to make sure the finally block is executed, so it does that. The idea is to execute the finally block and then come back and execute the return statement from the main block. But if you have a return statement in the finally block, then it will be executed when the finally block is executed... which means that control never returns to the main block to complete the return statement.

  1. The JVM encounters the return statement in the main block. It pauses execution of the main block and checks for a finally clause.
  2. It executes the finally clause in its entirety, including its return statement.
  3. It never thus gets to complete the try block.

Note, however, that the try block's return expression is evaluated and then discarded. This is important if it has side effects. So if your main block has return i++ then this will have no effect on the return value but i will still get incremented. (Thanks to Dirk for pointing this out.)




回答2:


If you have return in finally, that's the final return.

That is not surprising. It is actual behavior. Returning value decided at finally block.

If you are not returning anything in finally then the previous value of going to be return value is the returning value (in your case, the try block value).

Regardless of what you are doing in try, the finally block always executes even though you return from your try block (If you have return in finally, that's the final return).

From finally docs

The runtime system always executes the statements within the finally block regardless of what happens within the try block. So it's the perfect place to perform cleanup.

Note: finally designed to cleanup.




回答3:


In Java, the code:

try {
  if (foo()) return 1;
} catch (Exception e){
  if (goo()) return 2;
} finally {
  if (moo()) return 3;
}

will rewritten by the compiler into:

try {
  if (foo())
  {
    if (moo()) return 3;  // Finally code executed before return
    return 1;
  }
} catch (Exception e){
  if (goo())
  {
    if (moo()) return 3;  // Finally code executed before return
    return 2;
  }
} catch (Throwable e){
  if (moo()) return 3;   // Finally code executed before re-throw
  throw e;
}
if (moo()) return 3;    // Finally code executed before leaving block

Basically, the compiler will duplicate the code in the finally block exactly once in every execution path that would cause code execution to leave the guarded block, whether via return, throw, or fall-through. Note that while some languages disallow a return within a finally block, Java does not; if a finally block is executed as a consequence of an exception, however, a return within the block may cause the exception to get silently abandoned (look at the code above marked "Finally code executed before re-throw"; if the return 3; executes, the re-throw will get skipped).



来源:https://stackoverflow.com/questions/26658853/understanding-try-catch-finally-with-return-and-value-that-it-returns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!