Try-catch-finally-return澄清 [英] Try-catch-finally-return clarification
问题描述
通过阅读本论坛中已经提到的与上述主题相关的所有问题(见标题),我完全理解最后
总是被调用。 (除了 System.exit
和无限循环)。但是,我想知道是否在catch块中调用了 return
,然后从中调用了另一个 return
最后阻止。
例如:
public static void main( String [] args){
int a = new TestClass()。absorbbeTheValue();
}
int absorbbeTheValue(){
try {
int a = 10/0;
如果(a> 0)返回4;
} catch(例外e){
返回45;
}最后{
返回34;
}
}
所以输出(调用方法时)在任何情况下都将是34。这意味着终于总能运行。我认为虽然其他回归根本没有运行。在许多帖子中,我发现最终将内容写入catch子句返回已写入的内容。我的理解是,一旦catch子句中的返回值即将被计算,控制流就会传递给finally子句,而子句又有另一个返回,这次返回将被评估,而不会将控制权传递回catch子句。这样,在运行时调用的唯一返回
将是最终返回。你同意吗?
返回
最后
不会将控件传回程序,但返回值并终止方法。我们可以这样说吗?
如果返回
中的尝试
阻止,它将控制转移到 finally
块,并且函数最终正常返回(不是抛出)。 / p>
如果发生异常,但代码从 catch
返回返回 code>块,控制转移到
finally
块,函数最终正常返回(不是抛出)。
在您的示例中, finally
中有返回
,因此无论发生什么情况,函数将返回 34
,因为 finally
有最终的(如果你愿意)字。
虽然你的例子中没有涉及,但即使你没有 catch
并且如果在尝试
阻止并且不被捕获。通过从 finally
块执行 return
,可以完全禁止该异常。考虑:
public class FinallyReturn {
public static final void main(String [] args){
的System.out.println(FOO(参数));
}
private static int foo(String [] args){
try {
int n = Integer.parseInt(args [0]);
返回n;
}
最后{
返回42;
}
}
}
如果您在没有供应的情况下运行任何参数:
$ java FinallyReturn
... <$中的代码c $ c> foo 抛出 ArrayIndexOutOfBoundsException
。但是因为 finally
块执行返回
,该异常会被抑制。
这就是为什么最好避免在中使用
。返回
最后
By reading all the questions already asked in this forum related to the topic above (see title), I thoroughly understand that finally
gets always called. (except from System.exit
and infinite loops). However, I would like to know if a return
is called in a catch block and then another return
is called from the finally block.
For example:
public static void main(String[]args) {
int a = new TestClass().absorbeTheValue();
}
int absorbeTheValue() {
try {
int a = 10/0;
if (a > 0) return 4;
} catch(Exception e) {
return 45;
} finally {
return 34;
}
}
So here the output (when the method is called) is going to be 34 in any case. It means that finally always gets run. I think though that the other "returns" are not run at all. In many posts I found the fact that finally write the content over what had been already written by the catch clause return. My understanding is that as soon as the return value in the catch clause is about to be evaluated, the control flow pass to the finally clause which having in turn another return, this time the return will be evaluated without passing control back to the catch clause. In this way the only return
called at runtime will be the finally return. Do you agree with that?
A return
in finally
does not pass back the control to the program but returns the value and terminates the method. Can we say so?
If the return
in the try
block is reached, it transfers control to the finally
block, and the function eventually returns normally (not a throw).
If an exception occurs, but then the code reaches a return
from the catch
block, control is transferred to the finally
block and the function eventually returns normally (not a throw).
In your example, you have a return
in the finally
, and so regardless of what happens, the function will return 34
, because finally
has the final (if you will) word.
Although not covered in your example, this would be true even if you didn't have the catch
and if an exception were thrown in the try
block and not caught. By doing a return
from the finally
block, you suppress the exception entirely. Consider:
public class FinallyReturn {
public static final void main(String[] args) {
System.out.println(foo(args));
}
private static int foo(String[] args) {
try {
int n = Integer.parseInt(args[0]);
return n;
}
finally {
return 42;
}
}
}
If you run that without supplying any arguments:
$ java FinallyReturn
...the code in foo
throws an ArrayIndexOutOfBoundsException
. But because the finally
block does a return
, that exception gets suppressed.
This is one reason why it's best to avoid using return
in finally
.
这篇关于Try-catch-finally-return澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!