Java uncaught global exception handler

后端 未结 3 1216
北恋
北恋 2020-12-01 04:05

I have an application and need to code a custom global uncaught exception handler. I\'ve read all the stackoverflow threads and each one of them is just missing a clear and

相关标签:
3条回答
  • 2020-12-01 04:10

    You can use UncaughtExceptionHandler to handle the exception those causes a thread to terminate abruptly. Java doc for UncaughtExceptionHandler -

    Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments

    You need to implement this interface and set your implementation of UncaughtExceptionHandler for a thread by using method setUncaughtExceptionHandler. There are two ways you can do this -

    1. setUncaughtExceptionHandler
    2. setDefaultUncaughtExceptionHandler

    1) setUncaughtExceptionHandler - This will be a thread specific exception handler. So in case this thread gets terminated by some unhandled exception, this handler will be used.

    2) setDefaultUncaughtExceptionHandler - This will be a default exception handler in case there is no specific uncaught exception handler for a thread.

    Example -

    class MyExceptionHandler implements UncaughtExceptionHandler{
        @Override
        public void uncaughtException(Thread arg0, Throwable arg1) {
            System.out.println("[DEFAULT EXCEPTION HANDLER] Caught some exception");
        }
    }
    
    class MyThread extends Thread{
        public MyThread() {
            setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    System.out.println("[THREAD SPECIFIC] My exception handler");
                }
            });
        }
        public void run(){
            throw new RuntimeException();
        }
    }
    
    public class Test {
    
        public static void main(String[] args) {
            Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
            new MyThread().start();
            // current thread does not have a thread specific exception handler
            throw new RuntimeException();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 04:18

    You can try setting your own uncaught Exception handler, however I strongly advise against it. If your application has parts that can potentially break and if that exception is then not properly handled at the location where your code breaks, your entire program will malfunction frequently.

    0 讨论(0)
  • 2020-12-01 04:24

    You can set UncaughtExceptionHandler for the thread controlling the code above:

    // t is the parent code thread
    t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    
        public void uncaughtException(Thread t, Throwable e) {
           LOGGER.error(t + " throws exception: " + e);
        }
     });
    

    Java docs for UncaughtExceptionHandler -

    When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments

    the setUncaughtExceptionHandler is commonly used to free memory or kill threads that the system will not be able to kill, and perhaps, remain zombie.

    A real example:

    Thread t = new Thread(new MyRunnable());
    t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    
        public void uncaughtException(Thread t, Throwable e) {
           LOGGER.error(t + " throws exception: " + e);
        }
    });
    t.start();
    
    //outside that class
    class MyRunnable implements Runnable(){
        public void run(){
            throw new RuntimeException("hey you!");
        }
    }
    
    0 讨论(0)
提交回复
热议问题