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.

0

1 Answer 1

0

You can throw it with a little hack but you will get ExceptionInInitializerError:

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

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

java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException
...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.