相关疑难解决方法(0)

finally块总是在Java中执行吗?

考虑到这一点的代码,我可以绝对肯定的是,finally块总是执行,不管something()是什么?

try {  
    something();  
    return success;  
}  
catch (Exception e) {   
    return failure;  
}  
finally {  
    System.out.println("I don't know if this will get printed out");
}
Run Code Online (Sandbox Code Playgroud)

java return try-catch-finally

2281
推荐指数
38
解决办法
48万
查看次数

在java中,如果try和catch都抛出相同的异常并最终返回怎么办?

public class Abc {
    public static void main(String args[]) {
        System.out.println(Abc.method());
    }

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

为什么返回值为4?

java exception-handling

4
推荐指数
1
解决办法
5151
查看次数