2
public class HelloWorld {
   static {
    try {
        int i=10/0;
     } catch(ExceptionInInitializerError | ArithmeticException e ) {
            e.printStackTrace();
     }
   } 

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

Output:

java.lang.ArithmeticException: / by zero                                                                                                                                                                                                         
        at HelloWorld.<clinit>(HelloWorld.java:7)

There is no actual purpose to this code. But just wondered why it threw ArithmeticException over ExceptionInInitializerError. Just trying out multi-catch statement and ran into this.

The code below throws ExceptionInInitializerError. So logically, if I use try-multicatch, it should catch ExceptionInInitializerError, but its not the case here. Can anyone help me out here.

public class HelloWorld {

     static int i = 10/0;

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

Output:

Exception in thread "main" java.lang.ExceptionInInitializerError                                                                                                   
Caused by: java.lang.ArithmeticException: / by zero                                                                                                                
        at HelloWorld.<clinit>(HelloWorld.java:4) 
sinsuren
  • 1,745
  • 2
  • 23
  • 26
Akash Kaundinya
  • 109
  • 1
  • 6
  • As a guess, the real exception (`ArithmeticException`) is thrown first. When you don't catch it, it gets wrapped in `ExceptionInInitializerError` at the completion of the static initialization. If however you DO catch it you are immediately printing the stack trace _while still in the static initialization code_. Did you look further down to see if you then got `ExceptionInInitializerError`? – Jim Garrison Aug 03 '16 at 06:55

4 Answers4

3

When a static initializer of a class fails with an exception, an ExceptionInInitializerError that wraps the original exception is thrown. In your first snippet, there is no failure in the static initialization block - the same ArithmeticException is thrown by attempting to perform 10/0, but it is caught and not allowed to propagate out of the initialization block, so no ExceptionInInitializerError is generated.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

The code i=10/0 throws an ArithmeticException.

When you use that without a try-catch, then there is nothing to handle that exception. An uncaught exception while initializing causes an ExceptionInInitializerError. As you can see, the error carries the original ArithmeticException as its cause.

But when you have try-catch, then you no longer have this problem. There is no unhandled exception in the code - it is handled by your try-catch. Thus, you see the original exception that would have caused the error.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
2
static
{
    try
    {
        int i = 10 / 0;
    }
    catch (ExceptionInInitializerError | ArithmeticException e)
    {
        e.printStackTrace();
    }
}

This causes an ArithmeticException but since you catch it, there is not problem with the Initialization.

static
{
    int i = 10 / 0;
}

This causes an ArithmeticException but since you do not catch it, the ArithmeticException causes an ExceptionInInitializerError. You can see it from the stack shown below.

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
    at src.Test.<clinit>(Test.java:23)     
kai
  • 6,702
  • 22
  • 38
0

The state i=10/0 throws only ArithmeticException , so your try catch catches the ArithmeticException not the ExceptionInInitializerError.

when there is no try catch it is caught by default Exception handler.

ravthiru
  • 8,878
  • 2
  • 43
  • 52