[jdom-interest] [Vote] NoSuchChildException
Jools Enticknap
jools at jools.org
Thu Jul 13 06:45:21 PDT 2000
> I get a very strong feeling of "We don't need no stinking exceptions"
> in this thread; and I don't think it's a rational response. I think
> they're a lot of programmers out there who don't like exceptions,
> never really understood them in the first place, and certainly don't
> feel comfortable with them now. I see this all the time in the
> students I teach, and one of the things I insist on in my classes is
> making sure my students use exceptions whether they want to or not.
Personally I have'nt noticed any Exception xenophobia in the discussion so
far.
As far as making students comfortable with exceptions, I tend to take the
route via design princibles rather than stuff it down their necks.
Design princibles promote the need for an Exception, when it fits.
> I think they're a lot of programmers who don't want to have to think
> about how to handle exceptions. Returning null lets them ignore the
> problem and pretend it's gone away. All claimed benchmarks aside,
> that's its biggest attraction.
The biggest attraction is that I can decide how I want to handle the
situation where the information I requested is not available.
I don't think that anybody is going to be foolish enough to expect a NPE
to "go away".
> Throwing the exception is the right thing to do. It requires
> programmers to handle the case where the element isn't there. The
> reason checked exceptions exist is precisely because programmers
> can't be relied on to check error codes and return values. That's
> exactly what we're dealing with here. Whichever way the vote goes,
> this method will cause exceptions. The only question is whether these
> will be NoSuchChildExceptions that will be caught in a relevant catch
> block, or uncaught NullPointerExceptions that bring the whole program
> down.
I tend to follow the axiom that an exception should be thrown when an
abnormal or exception condition has occured. Requesting an Element which
does not exists is not an exceptional condition, nor is it abnormal.
But if the exception is designed to stop myopic programers making
mistakes, then I suggest that instead of checking for null they might just
make the next cardinal sin;
Element e =null;
try { e = doc.getChild("foo"); } catch( Exception e ) {}
Because incompetence breeds incompetence.
--Jools
More information about the jdom-interest
mailing list