Java.Core.What exception is thrown when an error occurs in a class initializer?

When an error occurs in a class initializer (static initializer block or static variable initialization), Java throws a ExceptionInInitializerError.

What is ExceptionInInitializerError?

  • It is a subclass of Error, meaning it is an unchecked exception.
  • It occurs when an exception is thrown inside a static block or while initializing static variables.
  • The root cause is often a RuntimeException (e.g., NullPointerException, ArithmeticException) that occurs during static initialization.

Example 1: ExceptionInInitializerError due to division by zero

public class StaticInitializerErrorExample {
    static int value = 10 / 0; // Causes ArithmeticException

    public static void main(String[] args) {
        System.out.println("This will never execute.");
    }
}

Output:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
    at StaticInitializerErrorExample.<clinit>(StaticInitializerErrorExample.java:2)

Here, ExceptionInInitializerError wraps the original ArithmeticException.


Example 2: ExceptionInInitializerError due to NullPointerException

public class StaticInitializerExample {
    static String text = null;
    
    static {
        System.out.println(text.length()); // Throws NullPointerException
    }

    public static void main(String[] args) {
        System.out.println("This will never execute.");
    }
}

Output:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at StaticInitializerExample.<clinit>(StaticInitializerExample.java:6)

Here, the NullPointerException occurs inside a static block, which causes ExceptionInInitializerError.


How to Handle ExceptionInInitializerError?

  • Ensure that static initializers don’t perform risky operations.
  • Use try-catch inside static blocks to catch exceptions before they cause an ExceptionInInitializerError.

Example: Handling Exception in Static Block

public class SafeStaticInitializer {
    static {
        try {
            int result = 10 / 0; // Risky operation
        } catch (ArithmeticException e) {
            System.out.println("Handled exception in static block: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println("Program continues...");
    }
}

Output:

Handled exception in static block: / by zero
Program continues...

Here, the exception is caught, preventing the program from crashing.


Key Takeaways

ExceptionInInitializerError occurs when an exception is thrown in a static block or static variable initialization.
✅ It wraps the actual exception that caused the error (e.g., ArithmeticException, NullPointerException).
✅ It terminates the program if not handled properly.
✅ Use try-catch inside static blocks to prevent crashes.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.