magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

I’ve been looking to simplify some exception handling code recently, namely I wanted to get rid of much of the defencive exception handling placed in event handlers in a Swing UI. Code like this:



public void actionPerformed(ActionEvent event) {
  try{
    //Do something
    ...
  }catch(Exception e){
    //handle exception
    ...
  }
}



What the code is trying to do is make sure is that any exception thrown is brought to the attention of the user. I’ve been looking at a few approaches to help catch and handle these exceptions without the developer having to explicitly catch them at the UI level.

Create your own @EventQueue@

You can create your own @EventQueue@, and have it catch uncaught exceptions when dispatching methods, like so:



public class ExceptionHandlingEventQueue extends EventQueue {
  public void dispatchEvent(AWTEvent event) {
    try {
      super.dispatchEvent(event);
    } catch (Exception e) {
      //handle exception
      ...
    }
  }
}


To use your new @ExceptionHandlingEventQueue@, install it like so:



Toolkit.getDefaultToolkit().getSystemEventQueue().push(this);


This will let you catch exceptions thrown directly from event code, so using a contrived example, this event handler will throw and exception that our @dispatchEvent@ will catch:



public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Method not implemented");
}


If however an operation is started in its own @Thread@ and then throws an exception, we’ll be unable to catch it – in this example the exception will not be caught by our @dispatchEvent@ method:



public void actionPerformed(ActionEvent e) {
  Thread t = new Thread() {
    public void run() {
      throw new UnsupportedOperationException("Method not implemented");
    }
  };
  t.start();
}


Create your own @ThreadGroup@

Another approach is to start you UI within a subclasses @ThreadGroup@, and override its @uncaughtException@ method:



public class ExceptionHandlingThreadGroup extends ThreadGroup {
  public void uncaughtException(Thread t, Throwable e) {
    //handle exception
    ...
  }
}


Now, if you start and of your threads as a child of a @ExceptionHandlingThreadGroup@, any uncaught exception will be caught and handled. If you abstract out your @Thread@ creation by using a factory, making all @Thread@ instances a child of your own @ThreadGroup@ shouldn’t be too much bother…

Or just wait for Java 5

In Java 5, the @Thread@ class adds support for adding uncaught exception handlers, using @Thread.setDefaultUncaughtExceptionHandler@ and @Thread.setUncaughtExceptionHandler@. From the Java 5 API:

@public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)@

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

A thread can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set. If no such handler is set then the thread’s ThreadGroup object acts as its handler.

Advertisements

One Response to “Catching uncaught exceptions”

  1. R.J.

    Just for reference, Netbeans uses the ThreadGroup approach to handle ‘when all else fails’ sort of exceptions.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: