I am using a class called MyExceptionHandler that implements Thread.UncaughtExceptionHandler to handle normal exceptions in my project.
As I understand this class can’t catch the EDT exceptions, so I tried to use this in the main() method to handle EDT exceptions:
public static void main( final String[] args ) {
Thread.setDefaultUncaughtExceptionHandler( new MyExceptionHandler() ); // Handle normal exceptions
System.setProperty( "sun.awt.exception.handler",MyExceptionHandler.class.getName()); // Handle EDT exceptions
SwingUtilities.invokeLater(new Runnable() { // Execute some code in the EDT.
public void run() {
JFrame myFrame = new JFrame();
myFrame.setVisible( true );
}
});
}
But untill now it’s not working. For example while initializing a JFrame I load its labels from a bundle file in the constructor like this:
setTitle( bundle.getString( "MyJFrame.title" ) );
I deleted the key MyJFrame.title from the bundle file to test the exception handler, but it didn’t work! The exception was normally printed in the log.
Am I doing something wrong here?
The EDT exception handler doesn’t use
Thread.UncaughtExceptionHandler. Instead, it calls a method with the following signature:Add that to
MyExceptionHandler, and it should work.The “documentation” for this is found in
EventDispatchThread, which is a package-private class injava.awt. Quoting from the javadoc forhandleException()there:How exactly Sun expected you find this, I have no idea.
Here’s a complete example which catches exceptions both on and off the EDT:
That should do it.