It looks to me that you got caught by poor design. Why chain
exceptions? If is a checked exception declare it in the throws and let
the proper method (See: Design By Responsiblity) in the stack handle
it. If it an unchecked exception (like NPE), some ultimate authority
(main route, servlet, etc) needs to catch it and handle it. Its much
simpler and causes a lot let head scratching.
> It looks to me that you got caught by poor design. Why chain
> exceptions? If is a checked exception declare it in the throws and let
> the proper method (See: Design By Responsiblity) in the stack handle
> it. If it an unchecked exception (like NPE), some ultimate authority
> (main route, servlet, etc) needs to catch it and handle it. Its much
> simpler and causes a lot let head scratching.
To be fair, I've seen some situations in which exception chaining (in my
humble opinion) made sense. You might have a tiered plugin design, for
example, where each plugin catches all the exceptions of their children
plugins, tries to recover from them if possible, but otherwise wraps the
exception and rethrows it, so that a parent plugin can try to handle it. A
lot of Eclipse is built that way.
Another example is when you know that a given exception thrown from a
given 3rd party library means something specific, and you'd like to add that
specific information to whatever is higher up in the callstack (which may
possibly be the end user). Perhaps your 3rd party library FrotzBlaster
throws a NullPointerException when passed a FrotzReferer that doesn't point
to a Frotz that actually exists on the Network, but finding out whether the
Frotz exists or not is expensive and has various side effects. So you read
in the "Frotz=fred" line from the configuration file, but you don't actually
validate the Frotz until the user request it to be blasted. When the user
DOES request it to be blaster, you pass the Frotz over to the library, which
then throws a NPE. You catch it, and wrap it in an different exception which
actually explains that the source of the error is an incorrect setting in
the configuration file.
- Oliver
jladd@bigpond.net.au - 17 Jan 2006 19:49 GMT
Good comments Oliver.
Chaned exceptions make sense.
Now using Exceptions to control program flow that could have been
handled by a conditional statement, thats just wrong.
I know this wasnt mentioned in the above posts but I think its an
important enough practice that it warranted mentioning.
Rgs, James.
http://www.jamesladdcode.com/moat