Wednesday 23 March 2011

Coding WTF: Throw an exception to trace callers of a method

I found an interesting bit of code at work:


try { // This will allow us to debug future cache reloads
throw new Exception("Issuer Security Cache was Reloaded");
} // Stack Trace gives context to see where it was initiated from
catch(Exception e) {
log.debug(e, e);
}

It was a bit of a WTF, and it probably still is from a performance
point of view, but I can see why they did this.

I guess this was written before they had IDEs that could find out the
callers for a particular method. But couldn't they have just done
that anyway by grepping through the code base? Oh, right, inheritance
4 levels deep can leave you unable to find out which actual class
you're looking at.