Try with multiple catches


public void doStuff()
{

  try {
    doSomethingThatCouldBreak();
  } catch (InvocationTargetException ite) {
    logger.error("Caught error", ite);
    handleError(ite);
  } catch (IOException ioe) {
    logger.error("Caught error", ioe);
    handleError(ioe);
  } catch (SecurityException se) {
    logger.error("Caught error", se);
    handleError(se);
  }
}

Code like this really suggests that each of these exceptions will be a different scenario for your code. The list of potential exceptions, including RuntimeExceptions, is almost endless. If the way each exception is handled is the same don’t try to be cute picking and choosing your exceptions – catch Exception. If you catch it below the top-level of the application (which is where you normally handle exceptions that have bubbled up), you have typically got 2 things to do – log it, throw it wrapped in a RuntimeException or similar.

If you are writing more code for error handling than for functionality you are probably doing something wrong.

Advertisements

One thought on “Try with multiple catches

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