How to handle exception thrown from static block in java? Why throw not allowed in static block

How to handle exception or error thrown from static block or initializing static variable in java?
Why throw not allowed in static block in java?
Java does not allows static initialization block to throw any exception, though it is allowed to use use try-catch block inside static initialization block.



First of all I will like to discuss about what are static blocks in java? what are features of static block in java?


  • static block are also known as static initialization blocks in java.
  • Any code written inside static block is thread safe.
  • Only static variables can be accessed inside static block
  • static blocks can be used for initializing static variables or calling any static method.
  • this keyword cannot be used in static block.

Why throw not allowed in static block?
We don’t have any control over the execution of static blocks because they are executed when class is loaded.
We cannot call static block at our will i.e. after class has been loaded or in between program execution. So, throwing exception from static initialization block does not make any sense.

How to handle exception or error thrown from static block or initializing static variable in java?
If any RuntimeException occurs in static initialization block of class then java.lang.ExceptionInInitializerError is thrown. So, we can catch ExceptionInInitializerError and handle exception thrown from static block in java.
We will create simple program to understand how RuntimeException occured in static initialization block of class can throw ExceptionInInitializerError and how to handle it.


public class SolveStaticBlockExceptionExample {
public static void main(String[] args) {
     try {
          new A(); // It will throw java.lang.ExceptionInInitializerError
     } catch (Throwable error) {
          error.printStackTrace();
     }
}
}
class A {
static { // static block of class, which will throw RuntimeException
     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
java.lang.ExceptionInInitializerError
at SolveStaticBlockExceptionExample.main(SolveStaticBlockExceptionExample.java:4)
Caused by: java.lang.ArithmeticException: / by zero
at A.<clinit>(SolveStaticBlockExceptionExample.java:14)
... 1 more


In the above program, first new A() at line 4 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.


What static initialization blocks allows regarding Exceptions in java?
  • Java allows to use use try-catch block inside static initialization block.




What static initialization blocks doesn’t allows?
  • Java does not allows static initialization block to throw any exception in java.


  • Explicitly it does not allow you to throw even RuntimeException . Example - throw new RuntimeException() is not allowed.



Related >>

Static keyword in java - variable, method, class, block - 24 salient features



Differences between Instance initialization block and Static initialization block in java - Features in detail with programs

Exception handling, exception hierarchy in java


Differences between throw and throws in java



try catch finally block in java



Why Static method cannot be overridden in java - Explanation with program


eEdit
Must read for you :