5

I'm unsure why the uncaughtException method is not being invoke.

static
{
    /**
     * Register a logger for unhandled exceptions.
     */
    Thread.UncaughtExceptionHandler globalExceptionHandler = new Thread.UncaughtExceptionHandler()
    {
        @Override
        public void uncaughtException(Thread t, Throwable e)
        {
            System.out.println("handle exception."); // can also set bp here that is not hit.
        }
    };

    Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
    Thread.currentThread().setUncaughtExceptionHandler(globalExceptionHandler);

    /**
     * Register gateway listen port.
     */
    try
    {
       // some stuff that raises an IOException
    }
    catch (IOException e)
    {
        System.out.println("Throwing exception");
        throw new RuntimeException(e);
    }

}

The program output is:

Throwing exception

java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: blah.jks 
    (The system cannot find the file specified)
...some stack trace...
Exception in thread "main" 
Process finished with exit code 1
slavoo
  • 5,798
  • 64
  • 37
  • 39
Dejas
  • 3,511
  • 4
  • 24
  • 36
  • Statis section is threated specially in JVM as that's initial section for class so I think it really depends on JDK implementation. For examply my Oracle JDK 1.7.0_65 correctly handles exception. – terma Dec 29 '14 at 21:21
  • Probably because the exception is handled correctly, i.e. the thread is terminated. I would not use this handling unless you are actually generating threads outside the main thread. – Maarten Bodewes Dec 29 '14 at 21:31

3 Answers3

6

The RuntimeException being raised from a static initializer, it happens when your main class is loaded. It is then caught by the system class loader, which wraps it into an ExceptionInInitializerError, then exits from the JVM. Since the exception is caught, your default uncaught exception handler is never invoked.

Lolo
  • 4,277
  • 2
  • 25
  • 24
0

Your code is throwing an IOException, and your catch catches an IOException. The IOException is caught and handled. IIRC the UncaughtExceptionHandler only deals with uncaught exception from within normal code, not from within a catch. Try changing your catch temporarily to catch some other exception, and see what happens. Don't forget to change it back afterwards!

rossum
  • 15,344
  • 1
  • 24
  • 38
0

Your code is in static block. Unless on very rare case of JVM implemenation ( if any), static block is not where you should handle any errors or exceptions if possible. This is because you don't have that much of control on the execution of static block ( unless you have dynamic class loader), which is pretty rare.

So if it is fair, move your code to instance block and it should work fine.

So when something unexpected happens in your static block, your application is expected not to continue. So basically , all those unexpected exceptions in static block will be represented by ExceptionInIntiializerError. You can refer to here

Jimmy
  • 2,589
  • 21
  • 31