How can I catch AWT thread exceptions in Java?

后端 未结 4 1056
时光取名叫无心
时光取名叫无心 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:20

    There is a distinction between uncaught exceptions in the EDT and outside the EDT.

    Another question has a solution for both but if you want just the EDT portion chewed up...

    class AWTExceptionHandler {
    
      public void handle(Throwable t) {
        try {
          // insert your exception handling code here
          // or do nothing to make it go away
        } catch (Throwable t) {
          // don't let the exception get thrown out, will cause infinite looping!
        }
      }
    
      public static void registerExceptionHandler() {
        System.setProperty('sun.awt.exception.handler', AWTExceptionHandler.class.getName())
      }
    }
    

提交回复
热议问题