Friday, May 18, 2007

Exception handling and clutter log files

Ran across this in a client's today

try {...}
catch (Exception ex) {
LOGGER.error("updateDropShipPriceOnImport()", ex);
throw ex;

Now, why would you log the error only to rethrow it? I'd assume that if you're rethrowing it, something else is/should catch this and handle/log it. So then isn't this just unnecessary logging?

1 comment:

anjan bacchu said...

hi there,

There is NO SANE logic for it.
But there is a logic for it, though.(long explanation)

I worked on a codebase where we used XML Appender (java util logging). We were chaining exceptions(SQLException to say vendorException). The Xml appender in sun's java has a bug(1.4 and 1.5 ; don't know about 1.6) where the exception source stack trace(say, SQLException) is missing in the log file!!!

So, we started logging at the place where we throw(catch) SQLException since identifying the exact nature of the problem should not take much further time.