Debug exceptions in AWT queue thread

后端 未结 3 452
执念已碎
执念已碎 2020-12-21 10:47

I am developing a Swing application with a component performing custom painting. When I make some mistake in the painting code and an exception is thrown, the situation is h

3条回答
  •  甜味超标
    2020-12-21 11:21

    In Java

    Your problem is that the exception is being thrown in another thread, the event dispatch thread. A couple of solutions:

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Uncaught exception in thread: " + t.getName, e);
        }
    });
    

    In any case you should in principle but your UI startup code in a

    EventQueue.invokeLater();
    

    or SwingUtilities.invokeLater() that directly call this.

    In Scala

    Your problem is that the exception is being thrown in another thread, the event dispatch thread. A couple of solutions:

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
      def uncaughtException(t: Thread, e: Throwable) {
        logger.error("Uncaught exception in thread: " + t.getName, e)
      }
    })
    

    In any case you should in principle but your UI startup code in a

    EventQueue.invokeLater()
    

提交回复
热议问题