了解 try catch finally 及其返回的返回值和值 [英] Understanding try catch finally with return and value that it returns

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

问题描述

我有以下代码.

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

现在我知道 finally 总是被执行,除非我们调用 system.exit(0);调用或 JVM 崩溃.我无法理解的是为什么返回值发生了变化?我仍然希望它返回 try 块的值.
谁能解释为什么要考虑finally值而不是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 块中有 return 也会执行 finally ...或者 finally 仅在存在 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 块被执行,所以它会这样做.这个想法是执行 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. 它完整地执行 finally 子句,包括它的 return 语句.
  3. 它永远无法完成 try 块.
  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.

但是请注意,try 块的return 表达式被评估,然后被丢弃.如果它有副作用,这很重要.所以如果你的主块有 return 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 finally 及其返回的返回值和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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