Dan*_*ski 46 java return finally
鉴于此代码:
String test() {
try {
return "1";
} finally {
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
语言规范是否定义了调用的返回值test()
?换句话说:每个JVM中它总是一样的吗?
在Sun JVM中,返回值是2
,但我想确定,这不依赖于VM.
Rom*_*las 17
除以下示例外,将始终执行finally块:
String test() {
try {
System.exit(0);
} finally {
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,JVM将停止,而不执行finally
块.
所以在你的例子中,返回值将是2
.
是的,如果您从finally
块中返回一些内容,它将替换您从块try
或catch
块中返回的任何内容.
例外情况也是如此.如果你在finally
块中抛出一些东西,该异常将替换try
or catch
块中抛出的任何异常.所以要小心不要在finally
块中扔东西,因为它可能隐藏失败的原始原因.
读取程序的ByteCode后,代码如下:
finally 块语句内联在try 块的return 语句之前,因此finally 块的return 语句首先执行,而原始return 语句永远不会执行。
对于程序:
String test() {
try {
System.out.println("try");
return "1";
} finally {
System.out.println("finally");
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
它转换为:
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";
}
Run Code Online (Sandbox Code Playgroud)
对于程序:带有 catch 块:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
转换为:
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";
}
Run Code Online (Sandbox Code Playgroud)
注意:使用 JDK 1.7 编译并使用 Cavaj 反编译。