在finally块中,我可以告诉抛出了什么异常吗?

sau*_*123 2 java exception try-catch-finally

在finally块中,我可以告诉抛出了什么异常吗?

我理解,如果抛出异常,我们可以在finally块中进行验证.

Ale*_*ica 6

我无法想象这种情况应该是明智的做法,但你可以尝试这样的事情:

class Main {
    public static void throwsException() throws Exception {
        throw new Exception();  
    }

    public static void main(String[] args) {
        Exception caughtException = null;

        try {
            throwsException();
        }
        catch (Exception e) {
            caughtException = e;
            e.printStackTrace();
        }
        finally {
            System.out.println(caughtException);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)