小编ank*_*kur的帖子

java中的try块中的return语句和异常

public class Test2 {
    public static void main(String args[]) {

        System.out.println(method());
    }

    public static int method() {
        try {
            throw new Exception();
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个问题中try块有return语句并抛出异常...它的输出是COMPILER ERROR ....

我们知道finally块会覆盖try/catch块中的return或exception语句...但是这个问题在try块中都有...为什么输出是错误的?

java exception-handling try-catch

1
推荐指数
2
解决办法
9850
查看次数

标签 统计

exception-handling ×1

java ×1

try-catch ×1