简体   繁体   English

在java中的finally块后,return语句如何工作?

[英]How does a return statement work after a finally block in java?

Here's an example from the book "Java All-in-one desk reference" 这是“Java All-in-one桌面参考”一书中的一个例子

public class CrazyWithZeros {
public static void main(String[] args) {
    try {
        int answer = divideTheseNumbers(5, 0);
    } catch (Exception e) {
        System.out.println("Tried twice, still didn't work!");
    }
}

public static int divideTheseNumbers(int a, int b) throws Exception {

    int c;
    try {
        c = a / b;
        System.out.println("It worked!");
    } catch (Exception e) {
        System.out.println("Didn't work the first time.");
        c = a / b;
        System.out.println("It worked the second time!");
    } finally {
        System.out.println("Better clean up my mess.");
    }
    System.out.println("It worked after all.");
    return c;

}

} }

After the finally clause executes, the ArithmeticException is thrown back up to the calling method. 执行finally子句后,ArithmeticException将被抛回到调用方法。 The statement System.out.println("It worked after all."); 语句System.out.println("It worked after all."); would never be executed in this case. 在这种情况下永远不会被执行。 But what happened to the return c; 但是return c;发生了什么return c; ?

I wonder whether the return statement would still return the result of the division or not? 我想知道返回陈述是否仍会返回分裂的结果?

======== ========

I tried to replace " System.out.println("Better clean up my mess."); " with " System.out.println(c); ", then it's compiled and the results are as follows: 我试图替换“ System.out.println("Better clean up my mess."); ”with“ System.out.println(c); ”,然后它被编译,结果如下:

 Didn't work the first time. 0 Tried twice, still didn't work! 

I can't believe the variable c could be calculated. 我无法相信变量c可以计算出来。 (it's the wrong number, though) Why could this happen? (但这是错误的数字)为什么会发生这种情况?

Then I also tried to replace " System.out.println("Better clean up my mess."); " with " return c; " and deleted the statements below the finally block, it's compiled again...Since the finally block is executed whether or not any exceptions are thrown by the try block or caught by any catch blocks, the return c; 然后我也尝试替换“ System.out.println("Better clean up my mess."); ”with“ return c; ”并删除finally块下面的语句,再次编译...由于finally块是执行是否由try块抛出任何异常或由任何catch块捕获,返回c; should be executed. 应该执行。 But here're the results: 但结果如下:

Didn't work the first time. 第一次没工作。

looks like c couldn't get returned... 看起来像c无法返回...

return c is not executed either. 也不执行return c。 It goes straight to the catch block in your main method. 它直接进入main方法中的catch块。

What do you expect performing an error-prone operation the second time? 您希望第二次执行容易出错的操作是什么? :) :)

It is gonna generate an exception of the same type you came in the catch block with, but at that time it would not be handled - you don't have another try-catch within this catch block. 它会生成一个与catch块中相同类型的异常,但是当时它不会被处理 - 你在这个 catch块中没有另一个try-catch

The finally is executed always regardless either an exception occurs or a normal process flow proceeds. 无论是否发生异常或正常的流程进行,都始终执行finally In your case, you come to the finally block with the exception and throw it to the caller ( main ) where it gets handled by its own catch block. 在你的情况下,你来到带有异常的finally块并将它抛给调用者( main ),在那里它由自己的catch块处理。

I wonder whether the return statement would still return the result of the division or not? 我想知道返回陈述是否仍会返回分裂的结果?

What do you want to return? 你想要什么回报? You haven't initialized the variable c and there is no correct record to this variable. 您尚未初始化变量c并且此变量没有正确的记录。 Therefore, Java doesn't allow to write "something unexpected or unpredictable" into the c . 因此,Java不允许在c写入“意外或不可预测的东西”。

  1. A method returns some value if it is executed without exception or error. 如果执行没有异常或错误,则方法返回一些值。
  2. finally block does not have any impact on whether return statement will be executed or not. finally块对return语句是否执行没有任何影响。 (of course, finally block should not throw any further exception) (当然, finally块不应该抛出任何进一步的异常)
  3. catch block determines whether that exception should be propagated further or handled within the method. catch块确定是否应该进一步传播该异常或在该方法中处理该异常。

In the given example, an ArithmeticException is thrown from try block and it will be handled by respective catch block. 在给定的示例中,从try块抛出ArithmeticException ,它将由相应的catch块处理。 As catch blocks again throws the same exception, given return statement would never execute. 由于catch块再次抛出相同的异常,因此return语句永远不会执行。

In short, return c; 简而言之, return c; is never executed in above program and variable c will be deleted as any other local variable. 永远不会在上面的程序中执行,变量c将被删除为任何其他局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM