[Advanced-java] How To Determine The Appropriate Exception?

David Rosenstrauch david.rosenstrauch at aleri.com
Mon Aug 11 15:18:44 2003


On Friday 08 August 2003 08:34 pm, Nikolaos Giannopoulos wrote:
> > From: advanced-java-bounces@lists.xcf.berkeley.edu
> > [mailto:advanced-java-bounces@lists.xcf.berkeley.edu]On Behalf Of David
> > Rosenstrauch
> >
> >
> > I can't argue specifically with your rationalization, but I guess
> > I feel like using ClassCastExceptions in this way smells a bit funny
> > to me - although I can't pinpoint specifically why.
>
> I think what I dis-like most about this implementation is the fact that by
> using using ClassCastException error handling one would expect that it's
> because the author would consider a class cast exception an error condition
> i.e. that there should not be an instnace of any other class returned
> (other than the expected one) and if it isn't what I expect it to be then
> there's a problem - but the default value exposes its true purpose which is
> that the author does indeed expect that the exception may occur in normal
> flow and has even provisioned for it.


Yes, that's right, Nikolaos.  Now that you've clarified:  yes, that is what 
bothers me about it.

Good Java coding specifies that you should use Exceptions for exceptional 
(error) conditions, and not for normal processing (or as a 
shortcut/efficiency).  For example, you could iterate through an array using 
a while(true) loop, and just exit when an ArrayIndexOutOfBoundsException is 
thrown, but that's generally frowned upon as poor Java coding.  The same 
pretty much goes here.


Thanks,

DR