多重返回:哪一个设置最终的返回值?
给定以下代码:
String test() {
try {
return "1";
} finally {
return "2";
}
}
语言规范是否定义了对 test()
调用的返回值?换句话说:在每个 JVM 中它总是相同的吗?
在 Sun JVM 中,返回值为 2
,但我想确定,这与 VM 无关。
Given this code:
String test() {
try {
return "1";
} finally {
return "2";
}
}
Do the language specifications define the return value of a call to test()
? In other words: Is it always the same in every JVM?
In the Sun JVM the return value is 2
, but I want to be sure, that this is not VM-dependant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,语言规范 定义“2”是结果。如果虚拟机以不同的方式执行此操作,则它不符合规范。
大多数编译器都会抱怨它。例如,Eclipse 会声称 return 块永远不会被执行,但这是错误的。
编写这样的代码是非常糟糕的做法,永远不要这样做:)
Yes, the language spec defines that "2" is the result. If a VM does it differently, it's not spec-compliant.
Most compilers will complain about it. Eclipse, for example, will claim that the return block will never be executed, but it's wrong.
It's shockingly bad practice to write code like that, don't ever do it :)
是的,Java 语言规范在这个问题上非常明确(14.20.2):
Yes, the Java Language Specification is very clear on this issue (14.20.2):
除了以下示例之外,finally 块将始终被执行:
在这种情况下,JVM 将停止,而不执行
finally
块。因此,在您的示例中,返回值将为
2
。The finally block will always be executed except in the following example:
In this case, the JVM will stop, without executing the
finally
block.So in your example, the return value will be
2
.是的,如果您从
finally
块返回某些内容,它将替换您从try
或catch
块返回的任何内容。对于异常也是如此。如果您在
finally
块中抛出某些异常,该异常将替换在try
或catch
块中抛出的任何异常。所以要小心,永远不要在finally
块中扔东西,因为它可能隐藏失败的原始原因。Yes, if you return something from the
finally
block, it will replace whatever you might have returned from thetry
orcatch
block.The same is true also for exceptions. If you throw something in the
finally
block, that exception will replace whatever exception was thrown in thetry
orcatch
block. So be careful to never throw something in thefinally
block, because it may hide the original reason for a failure.读取程序的ByteCode后,代码如下:
finally块语句内联在try块的return语句之前,因此finally块的return首先执行,而原来的return语句永远不会执行。
对于程序:
它转换为:
对于程序: 带 catch 块:
转换为:
注意:使用 JDK 1.7 和 1.7 编译。使用Cavaj反编译。
After reading the ByteCode of program, the code is as follows:
The finally block statements is inlined before the return statement of try block, so the return from the finally block executes first and the original return statement never does.
For Program:
It converts to:
And For program: with catch block:
Converts to:
Note: Complied using JDK 1.7 & Decompiled using Cavaj.
您可以参考下面的链接。我希望它能提供所有详细信息:
http ://www.programmerinterview.com/index.php/java-questions/will-finally-run-after-return/
它说finally块将始终执行,即使try或catch块有return语句。如果finally块也有return语句,那么这将覆盖try或catch块内的return语句,在这种情况下,try/catch中抛出的任何异常都将被丢弃(不好的方法)。
You can refer the below link. I hope it will provide all details:
http://www.programmerinterview.com/index.php/java-questions/will-finally-run-after-return/
It says finally block will always executes even try or catch block has return statement. And if finally block also has return statement then this will override the return statement which is inside try or catch block and in this case any exception thrown in try/catch will be discarded (bad approach).
上面的答案总结得很好,只是想在这里补充一点。
如果我们传递 1,0 那么上面的方法将抑制它抛出的异常并只返回结果。
已经说过,上面的代码不应该在生产环境中使用。
在finally块之后使用return语句将导致
Above answer's is very well summarized just want to add one more point here.
If we pass the 1,0 then the above method will suppress the exception thrown by it and just return the result.
Been said, the above code should not be used in production environment.
using the return statement after the finally block will result in