Solve java.lang.ExceptionInInitializerError in java

What is hierarchy of java.lang.ExceptionInInitializerError?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Error
  -java.lang.LinkageError
   -java.lang.ExceptionInInitializerError



ExceptionInInitializerError is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.ExceptionInInitializerError  is a Error in java. Error and its subclasses are regarded as unchecked exceptions .


What is ExceptionInInitializerError in java ?

ExceptionInInitializerError is thrown if any RuntimeException occurs in >
  • static initialization block of class or
  • during initialing of the static variables
then ExceptionInInitializerError is thrown.


Example/Programs/Scenarios where ExceptionInInitializerError may be thrown in java>


  1. If any RuntimeException occurs in static initialization block of class then ExceptionInInitializerError is thrown.


We will create simple program to understand how RuntimeException occured in static initialization block of class can throw ExceptionInInitializerError.


First let’s write java class >
package ExceptionInInitializerErrorPkg;
public class ExceptionInInitializerErrorExample {
public static void main(String[] args) {
     new A(); // It will throw java.lang.ExceptionInInitializerError
}
}
class A {
     //static block of class, which will throw RuntimeException
static {
     System.out.println("In static block of class A");
     System.out.println(1 / 0); // It will throw java.lang.ArithmeticException
}
/*
* Constructor of class A will never get executed
*/
A() {
     System.out.println("In constructor of class A");
}
}


Output of above program >
In static block of class A
Exception in thread "main" java.lang.ExceptionInInitializerError
at ExceptionInInitializerErrorPkg.ExceptionInInitializerErrorExample.main(ExceptionInInitializerErrorExample.java:5)
Caused by: java.lang.ArithmeticException: / by zero
at ExceptionInInitializerErrorPkg.A.<clinit>(ExceptionInInitializerErrorExample.java:12)
... 1 more


In the above program, first new A() at line 5 throwed ExceptionInInitializerError because ArithmeticException occurred in initializing the class (i.e. in static initialization block of class).


You must read ArithmeticException  is an unchecked exception and it is propagated automatically in java.


  1. If any RuntimeException occurs in initializing static variables of class then ExceptionInInitializerError is thrown.


We will create simple program to understand how RuntimeException occurred in initializing static variables of class can throw ExceptionInInitializerError.


First let’s write java class >
package ExceptionInInitializerErrorPkg2;
public class ExceptionInInitializerErrorExample {
public static void main(String[] args) {
     new A(); // It will throw java.lang.ExceptionInInitializerError
}
}
class A {
//initializing static variable, which will throw RuntimeException
static int i = 1 / 0; // It will throw java.lang.ArithmeticException
}


Output of above program >
Exception in thread "main" java.lang.ExceptionInInitializerError
at ExceptionInInitializerErrorPkg2.ExceptionInInitializerErrorExample.main(ExceptionInInitializerErrorExample.java:5)
Caused by: java.lang.ArithmeticException: / by zero
at ExceptionInInitializerErrorPkg2.A.<clinit>(ExceptionInInitializerErrorExample.java:11)
... 1 more



In the above program, first new A() at line 5 throwed ExceptionInInitializerError because ArithmeticException occurred in initializing the class (i.e. in static initialization block of class).

ArithmeticException  is an unchecked exception and it is propagated automatically in java.




How to avoid ExceptionInInitializerError in java?


  • Ensure that static initialization block of classes does not throw any RuntimeException.
  • Ensure that initializing static variables of classes does not throw any RuntimeException.


Consequences of ExceptionInInitializerError in java?
  • You must ensure that class does not throws java.lang.ExceptionInInitializerError because that is likely to be followed by NoClassDefFoundError.



RELATED LINKS>


Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file




unchecked exception are propagated automatically in java.

No comments:

Post a Comment