小编Ant*_*Roy的帖子

带返回的 Try-finally 块不会引发异常

在这段代码中,main 方法的 Catch 不会捕获运行时异常。执行finally块后,它应该已经转到main的异常块,但它没有。

 class FinallyDemo {
    static int m1(){
        try{
            System.out.println("Inside m1");
            throw new RuntimeException("hi");
        }
        finally {
            System.out.println("m1 finally");
            return 5;
        }
    }
    public static void main(String[] args) {
        try{
            System.out.println(m1());
        }catch (Exception e){
            System.out.println("main caught: "+ e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Inside m1
m1 finally
5
Run Code Online (Sandbox Code Playgroud)

java exception try-finally

0
推荐指数
1
解决办法
236
查看次数

标签 统计

exception ×1

java ×1

try-finally ×1