在try / catch块Java中返回语句

Dim*_*tri 3 java try-catch

假设我有以下内容:

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));

    }
}
Run Code Online (Sandbox Code Playgroud)

我想了解的是,为什么在使用我们的main方法的第一行时,“ All done”行没有执行? Marathon(100)

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

T.J*_*der 5

我想了解的是,为什么在使用我们的main方法的第一行时,“ All done”行没有执行?马拉松(100)

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

return true;
Run Code Online (Sandbox Code Playgroud)

...将控制权立即转移到finally块;在的端finally块,该函数返回(没有运行任何线以下finally块)。也就是说,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;
}
Run Code Online (Sandbox Code Playgroud)

更多关于returntrycatch:如果你发出一个return从内try,有一个finally块,它会立即将控制权转给finally块。到达该块的末尾时,该函数终止(除非在块之后嵌套任何代码,否则运行任何代码)。如果您来自,也会发生同样的事情。finallyfinallyreturncatch

因此,例如:

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;
Run Code Online (Sandbox Code Playgroud)

"Got here"无论如何,将始终输出;这就是finally条款的重点,它们总是被执行。

"Got here because of exception"仅在someOtherCondition为true时输出(并且将在之前输出"Got here"),在这种情况下,该函数通常以value返回1

"May not have gotten here"将不会被输出,如果任一someConditionsomeOtherCondition是因为如此,return在S trycatch块。

如果两个条件都不成立,则您会看到"May not have gotten here"后面跟着,"Got here"并且该函数返回3

请注意,returncatch区块意味着该函数返回正常(与值2时)someOtherCondition是真实的,它不会抛出。如果没有return,并且someOtherCondition为true,则将看到"Got here because of exception"and "Got here",然后该函数将以throw终止(根本没有返回值),并且不会输出"May not have gotten here"

最后但并非最不重要的:如果你有一个returnfinally该块,return“赢”:即使你在很finally块,因为你已经出台了return,一个return在finally块将取代它,使得函数返回值的finally说,而不是较早的版本。