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
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());
}
});
Looks like your only solution might be switching to Eclipse. :-) The other solutions require coding effort and stopping in the exception handler is not the same as stopping in the exact place where the exception is thrown.
With the following program I have no problems listening to caught/uncaught instances of RuntimeException
in Eclipse.
package lambda;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class AWTExceptionTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Test");
button.addActionListener(e -> { throw new RuntimeException(); });
frame.add(button);
frame.setSize(new Dimension(50, 50));
SwingUtilities.invokeLater(() -> frame.setVisible(true));
}
}
Here is how it looks in debug mode in Eclipse.
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()