0

Why JVM does not allow to throw Unchecked Exception from static block? But it still allows them implicitly (e.g: calling method on null object).

Note: The question is more academic and not from real life issue.

4

1 回答 1

0

你可以用一点技巧把它扔掉,但你会得到ExceptionInInitializerError

表示静态初始化程序中发生了意外异常。抛出ExceptionInInitializerError以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常。

static {
    try {
        throw new RuntimeException();
    }
    catch (IllegalArgumentException ignored){}
}

java.lang.ExceptionInInitializerError
原因:java.lang.RuntimeException
...

于 2018-02-07T14:05:58.583 回答