[Advanced-java] How To Determine The Appropriate Exception?
Pollard, Jim
Pollard at stats.com
Fri Aug 8 17:08:31 2003
i think you guys are actually saying the same thing : avoid using the base
class Exception because you may end up catching exceptions that shouldn't be
caught.
the usual practice is to chain catch clauses from more specific to less
specific handling the exception depending on what type is caught. sometimes
not catching exceptions or rethrowing them to a higher level where they can
be dealt with more intelligently is the best way to go. i think that
Runtime Exceptions are less often caught by programs, because they usually
are thrown in situations where recovery isn't possible.
> -----Original Message-----
> From: David Rosenstrauch [mailto:david.rosenstrauch@aleri.com]
> Sent: Friday, August 08, 2003 10:22 AM
> To: Advanced-java@lists.xcf.berkeley.edu
> Subject: Re: [Advanced-java] How To Determine The Appropriate
> Exception?
>
>
> On Friday 08 August 2003 10:15 am, KCE wrote:
> >
> > Yeah, in fact, you can use the Exception base class:
> >
> > try {
> > myObject.myMethod();
> > } catch(Exception e) {
> > e.printStackTrace();
> > }
> >
> > Just try to avoid doing this whenever possible! Its
> > really easy to wind up with exceptions flying everywhere
> > and NO IDEA where they came from!!!
>
>
> Yes, use the exception class.
>
> But IMHO your response is off-base on 2 other points:
>
> 1) "Just try to avoid doing this whenever possible!" I
> couldn't disagree
> more. There's no need to "try to avoid" using exceptions.
> Use them when
> they're appropriate - they make for a clean flow of code.
> Don't use them
> when they're not appropriate. I don't think I ever
> experienced this issue of
> "exceptions flying everywhere and NO IDEA where they came
> from", and I've
> been coding in Java for 8 years.
>
> 2) You should never do this:
>
> } catch(Exception e) {
> e.printStackTrace();
> }
>
> "Swallowing" an exception deep down in some method in the
> call stack instead
> of propogating it up the call stack is a really bad idea. It
> makes for
> extremely buggy code where something dies deep down
> somewhere, but the
> calling code (whether GUI or server) never knows about it or
> handles it
> properly. Furthermore, "handling" an exception by just
> printing the stack
> trace is pretty useless. A stack trace printed to the
> console is of no use
> to an end user. (Assuming they even know where to look to read it.)
>
>
> DR
>
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@lists.xcf.berkeley.edu
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
>