Skip to content
Advertisement

Handling an exception as a method argument

I am looking for a design pattern to handle Exception instances received as method arguments.

To put some context into the question, I am using GWT and have various asynchronous handlers that usually come in a form similar to:

public interface AsyncCallback<T> {

  void onFailure(Throwable caught);

  void onSuccess(T result);

}

So, the onFailure method receives a Throwable instance that I need to handle.

Now, I have a number of exceptions I can receive in this method, for example

  • ConstraintViolationException
  • TimeoutException
  • NoSuchElementException

In my handling code I could of course write the following:

void handleException(final Exception e) {
    if(e instanceof TimeoutException) {
        handleTimeout();
    } else if (e instanceof NoSuchElementException) {
        handleInvalidElement();
    } else {
        stopAndCatchFire();
    }
}

But to my eye, that looks very ugly. The large if..else if chain combined with heavy usage of instanceof seems like something that should be avoided.

I thought that maybe I could use the try...catch construct to handle the Exception using something like the following:

void handleException(final Exception e) {
    try {
        throw e;
    } catch (TimeoutException te) {
        handleTimeout();
    } catch (NoSuchElementException nsee) {
        handleInvalidElement();
    } catch (Exception ex) {
        stopAndCatchFire();
    }
}

But this seems like an abuse somehow. Do you see any downsides to the second approach or another approach I could go with to avoid the first case?

Advertisement

Answer

Could you not have a dictionary of exceptionHandlers keyed by the type of exception they handle, then when you get a exception you look in the dictionary for the handler for the exception type. If there is one, then pass the exception to the handler, if there isn’t then use the default handler.

So your handler becomes something like this:

void handleException(final Exception e) {
    if (handlers.containsKey(e.getType())
    {
        handlers[e.getType()].handle(e);
    }
    else
    {
         defaultHandler.handle(e);
    }
}

My Java is a bit rusty, so the example is c-sharpy but should be simple enough to translate (though I remembered not to capitalise the first letter of everything :))

This approach should have the advantage that you can add new handlers simply.

It will however suffer if you have the same handler for sub types, as you will have to register each subtype explicitly.

To get around this issue simply make each handler responsible for making the decision about whether it can handle an exception:

public interface ExceptionHandler
{
     bool canHandle(Exception e);
     void handle(Exception e)
}

then just put the handlers in a list an iterate asking each one if it can handle the current exception and when you find one that can, get it to handle it.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement