Debug exceptions in AWT queue thread

后端 未结 3 448
执念已碎
执念已碎 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:10

    According to this link, you have to handle both regular Exception and EDT Exception without using the old sun.awt.exception.handler hack (which does not work anymore since Java 7)

    Here is your ExceptionHandler

    public static class ExceptionHandler implements Thread.UncaughtExceptionHandler
    {
        public void uncaughtException(Thread thread, Throwable thrown)
        {
            // TODO handle your Exception here
        }
    }
    

    Usage :

    // 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());
        }
    });
    

提交回复
热议问题