Pragmatic Exceptions

Dr. Dobb's Journal Febrruary, 2006


Tip #6: Don’t Throw Logs

Other than in tornados, logs aren’t thrown. They’re sawed, chopped, rolled, turned, burned, floated, and even written—but never thrown. This simple physics applies to programming as well. In other words, you shouldn’t throw exceptions that have already been logged. And yet, I’ve seen code such as this:

try {
  // something that generates an exception
  . . .
  } catch( Exception x ) {
  Logger.log(x);
  throw x;
  }

Bad. This will most likely result in seeing the same exception message at very different points in your program’s execution. The problem is, it’s the same error!

Trying to debug this is confusing at best. It also sends an ambivalent and confusing message to the callers of your function. The pitcher is saying to the catcher, “I’ll log this now but, well, I’m not sure…it could be fatal…perhaps you should deal with it, too?” This isn’t only weak minded; it’s also lazy and pathetic.

—Benjamin Booth
http://www.benjaminbooth.com/