Try with multiple catches

public void doStuff()

  try {
  } catch (InvocationTargetException ite) {
    logger.error("Caught error", ite);
  } catch (IOException ioe) {
    logger.error("Caught error", ioe);
  } catch (SecurityException se) {
    logger.error("Caught error", 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.


One thought on “Try with multiple catches

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s