Re: Exception handling
In article <ab85fdb4-c347-4917-81e8-4d9a172b5078
@m11g2000vbo.googlegroups.com>, lew@lewscanon.com says...
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 u=
se
Lew wrote:
A) this does use a logger,
Pitch wrote:
Well, if you use Logger implemetation form Apache.org, this does exactl=
y
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?
Well, not exactly, but it's pretty much the same thing. Anyway I don't
see the purpose of your method (related to this topic). And if you
wanted to show chaining it's not necessarry since the original poster
already talked about it.
I'm sorry but I say you posted this code just to show how you can write
cool for-loops.
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.
Ok, you right, but I would add this: if you design is bad, you must use
any helper tools you can get!
And yes, I think this aproach is a product of a badly designed software.
Exception cause should be only referenced as to be able to list it in
the log files, for history/debug purposes, so to say.
According to the Law of Demeter, a class should not access other than
immediate classes.
http://en.wikipedia.org/wiki/Law_of_Demeter
And isn't exception-chinaing just another buzzword for keeping a simple
reference?
I'm gonna skip the rest of our "misunderstandings" conversation.
--
stirr your cofee properly