jLuger.de - Update on how to catch unhandled exceptions

On 2011-02-24 I've written about how to catch unhandled exceptions. I have to admit that the blog entry was based on my observations with a test program. When implementing the solution into the real application, testing showed that it isn't working for threads outside the EDT. An exception in such a thread will still kill the application.
I haven't tried UncaughtExceptionHandler as all threads we start directly have a common base class. These base class has an abstract doStuff method that is overridden in the thread implementation. With that I've just that to put a try-catch block around the call of this method. The caught RuntimeException will be rethrown in the EDT. This is realized with the help of SwingUtilities. The resulting code looks like this:
try {
  doStuff()
} catch(final RuntimeException e) {
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      throw e;
    }
  });
}

Tests showed that the rethrown exception is catched in the dispatchEvent method of EventQueue class. The stack trace was the original one and doesn't point to our catch block.

This solution has a problem with checked Exceptions. We don't allow one to be thrown in doStuff() method but there are ways to throw them without declaring them in the method. It's a hack that uses type erasure. I don't have a link but I've once read about it. And of course this solution doesn't catch errors. So you may want to try UncaughtExceptionHandler.