Understanding try catch finally with return and value that it returns(了解 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.
- JVM 在主块中遇到
return
语句.它暂停主块的执行并检查finally
子句. - 它完整地执行
finally
子句,包括它的return
语句. - 它永远无法完成
try
块.
- The JVM encounters the
return
statement in the main block. It pauses execution of the main block and checks for afinally
clause. - It executes the
finally
clause in its entirety, including itsreturn
statement. - 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 及其返回的返回值和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:了解 try catch finally 及其返回的返回值和值
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01