Return try/catch 块 Java 中的语句

Return statements in try/catch block Java

假设我有以下内容:

class NegativeException extends RuntimeException {
}

class ZeroException extends NegativeException {
}

class Driver {
    static boolean Marathon(int a) {
        try {
            if (a < 0)
                throw new NegativeException();
            else if (a == 0)
                throw new ZeroException();
            else if (a >= 42)
                return true;
            else
                return false;
        } catch (ZeroException e) {
            System.out.println("Use natural number");
        } finally {
            System.out.println("One last thing");
        }
        System.out.println("All done.");
        return false;
    }

    public static void main(String[] args) {
        /* One last thing */
        /* true */
        System.out.println(Marathon(100));


        System.out.println(Marathon(0));
        System.out.println(Marathon(-5));

    }
}

我想了解的是,为什么当我们的 main 方法的第一行被触发时 "All done" 行不执行? Marathon(100)

似乎执行了finally语句然后输出了return语句。我知道 finally 块将始终执行,无论发生什么。但是,我似乎无法理解 return 语句如何影响 try catch 块的流程。尝试从 try-cath-finally 个区块 return 时是否有一套适用的规则?

What I'm trying to understand is why doesn't the line "All done" not execute when use the first line of our main method is fired? Marathon(100)

因为 a >= 42 是真的,所以你这样做:

return true;

...立即将控制转移到 finally 块;在 finally 块的末尾,函数 returns( 没有 运行 任何行 以下 finally 块)。也就是说,return 不只是设置一个 return 值,它会在 运行 任何未完成的 finally 块之后终止函数。

如果你想继续执行,你会写入一个变量,然后在最后有一个 return:

static boolean Marathon(int a) {
    boolean rv = false;
    try {
        if (a < 0)
            throw new NegativeException();
        else if (a == 0)
            throw new ZeroException();
        else if (a >= 42)
            rv = true;
    } catch (ZeroException e) {
        System.out.println("Use natural number");
    } finally {
        System.out.println("One last thing");
    }
    System.out.println("All done.");
    return rv;
}

有关 trycatch 中的 return 的更多信息:如果您从具有 finallytry 中发出 return块,它立即将控制转移到 finally 块。当到达该块的末尾时,函数终止(没有 运行任何代码 finally块之后,除非你有嵌套的 finally 块或类似的)。如果你从 catch.

return 也会发生同样的事情

例如:

try {
    if (someCondition) {
        return 1;
    }
    if (someOtherCondition) {
        throw new Exception();
    }
}
catch (Exception e) {
    System.out.println("Got here because of exception");
    return 2;
}
finally {
    System.out.println("Got here");
}
System.out.println("May not have gotten here");
return 3;

"Got here"总是输出,无论如何;这就是 finally 子句的重点,它们 总是 被执行。

"Got here because of exception" 只会在 someOtherCondition 为真时输出(并且会在 "Got here" 之前输出),在这种情况下,函数 return 的值通常为1.

如果 someConditionsomeOtherCondition 为真,则

"May not have gotten here" 不会输出,因为 try 和 [=22] 中的 return =]块。

如果两个条件都不成立,您会看到 "May not have gotten here" 后跟 "Got here" 和函数 returns 3.

请注意,catch 块中的 return 表示函数 returns normally(值为 2) 当 someOtherCondition 为真时,它不会抛出。如果你没有 return 并且 someOtherCondition 为真,你会看到 "Got here because of exception""Got here" 然后函数将终止并抛出(没有return 值),并且不会输出 "May not have gotten here".

最后但同样重要的是:如果你在 finally 块中有一个 return,那么 return "wins":即使你在 finally 块,因为你已经发出了一个 return,finally 块中的一个 return 取代了它,使函数 return 成为 finally 所说的值,而不是更早的值一.