理解try catch最后返回它返回的值和值 [英] Understanding try catch finally with return and value that it returns

查看:138
本文介绍了理解try catch最后返回它返回的值和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。

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";
    }
}

此输出为

Executing try
Executing finally
Return finally value

如果我将我的finally块更改为不返回类似

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");
    }
}

然后输出

Executing try
Executing finally
Return try value

现在我明白,除非我们调用system.exit(0),否则最后总是执行;调用或JVM崩溃。
我无法理解的是为什么返回值已经改变了?我仍然希望它返回try块的值。
有谁可以解释为什么考虑到最终值而不是try块的返回值?

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 ?

请不要回答因为最终被执行即使在try块中有返回...或者最终只有在有system.exit(0)时才执行;调用或JVM崩溃。据我所知。

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;
}

输出:

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


推荐答案

在从主程序块返回之前,JVM必须确保最终块被执行,所以它就这样做了。我们的想法是执行 finally 块,然后返回并执行主块中的 return 语句。但是如果你在 finally 块中有 return 语句,那么它将在 finally 块被执行...这意味着控件永远不会返回主块来完成 return 语句。

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. JVM在主块中遇到 return 语句。它暂停执行主块并检查 finally 子句。

  2. 它最终执行 子句,包括 return 语句。

  3. 从来没有完成尝试阻止。

  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.

但请注意,尝试块的返回表达式被评估然后被丢弃。如果它有副作用,这很重要。因此,如果你的主要块有返回i ++ 那么这对返回值没有影响,但是 i 仍然会增加。 (感谢 Dirk 指出这一点。)

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.)

这篇关于理解try catch最后返回它返回的值和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆