Archive for September 28th, 2006

Must never do Antipatterns – Exception Handling

September 28, 2006

From java.net: Exception-Handling Antipatterns

Antipatterns
Log and Throw

Example:

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw e;
}

or

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw new MyServiceException(“Blah”, e);
}

or

catch (NoSuchMethodException e) {
e.printStackTrace();
throw new MyServiceException(“Blah”, e);
}

All of the above examples are equally wrong. This is one of the most annoying error-handling antipatterns. Either log the exception, or throw it, but never do both. Logging and throwing results in multiple log messages for a single problem in the code, and makes life hell for the support engineer who is trying to dig through the logs.

I see this in so many “commercial” products that I want my money back !!; makes debugging a pain the neck. Seeing the same trace logged from every tier again & again is not pretty.