Re: Exception handling
Lew wrote:
It could be said that chained exceptions are a better choice.
/** Throwable chainer.
* @param trow Throwable to chain.
* @param log Logger with which to log the chain.
*/
public static void chain( Throwable trow, Logger log )
{
for ( Throwable last = null, thr = trow;
thr != null && thr != last;
thr = (last = thr).getCause() )
{
log.debug( thr.getLocalizedMessage() );
}
}
Pitch wrote:
I don't get this. If you are just trying to log all causes, why not use
logger?
Lew wrote:
A) this does use a logger,
Pitch wrote:
Well, if you use Logger implemetation form Apache.org, this does exactly
what you code does (but in fewer lines):
log.error(this, null, trow);
Exactly what I coded? It uses getLocalizedMessage() and does not
display a stack trace? For all logging libraries? Are you quite
sure?
Lew:
B) the purpose isn't to show logging but chaining.
Pitch wrote:
So you say inheritance is not the way to go? We should look through the
chained-exceptions to find out if we need to handle a general exception
or not? Doesn't that clutter the code with loops and if-statements?
I said it was an alternative, and quite a useful one when the
exceptions are not hierarchically related. In general one cannot
ensure that all exceptions are hierarchically related, and when one
wraps and rethrows with a lower-level exception as a cause, chaining
is the only way to go.
Lew:
If you think I was "just trying to log all causes" then you missed the p=
oint.
Pitch wrote:
Well, why don't you explain if you think I misunderstood something? Or
is it that you think your time is too valuable for a simple explanation?
It was explained in my post to which you originally replied.
I said:
It could be said that chained exceptions are a better choice.
Clearly from that you can see that my point was about chained
exceptions, no?
Stow your sarcasm; it was you who missed the point, not I who failed
to explain it.
Pitch wrote:
Anyway I think you're completely wrong and I wouldn't suggest to anyone
to use this approach.
Too bad for the people you're advising, then. Exception chaining is a
very powerful tool and quite usually the only way to get at root
causes, and it's quite standard throughout the Java universe/
--
Lew