Exceptions: ExceptionInInitializerError wraps a failure from a static initializer

If a static field initializer or static block throws, the class fails to load and the JVM reports it as an ExceptionInInitializerError, an Error rather than an Exception, with the real cause reachable via getCause.

Code
class Broken {
    static final int VALUE = 10 / Integer.parseInt("0");
}
try {
    int v = Broken.VALUE;
    System.out.println("Unreachable: " + v);
} catch (ExceptionInInitializerError e) {
    System.out.println(e.getClass().getSimpleName() + " caused by " + e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage());
}
Output
ExceptionInInitializerError caused by ArithmeticException: / by zero
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms