How can I catch AWT thread exceptions in Java?

后端 未结 4 1048
时光取名叫无心
时光取名叫无心 2020-12-06 05:03

We\'d like a trace in our application logs of these exceptions - by default Java just outputs them to the console.

4条回答
  •  遥遥无期
    2020-12-06 05:42

    Since Java 7, you have to do it differently as the sun.awt.exception.handler hack does not work anymore.

    Here is the solution (from Uncaught AWT Exceptions in Java 7).

    // Regular Exception
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
    
    // EDT Exception
    SwingUtilities.invokeAndWait(new Runnable()
    {
        public void run()
        {
            // We are in the event dispatching thread
            Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());
        }
    });
    

提交回复
热议问题