[jdom-interest] [Vote] NoSuchChildException

Steve Odendahl steve.odendahl at central.sun.com
Wed Jul 12 16:45:11 PDT 2000

On Wed, Jul 12, 2000 at 01:51:36PM -0400 or thereabouts, Elliotte Rusty Harold wrote:
> -1 on returning null
> You asked for a reason for a -1 vote so I'll give you mine. I was 
> initially undecided, but when I actually looked at the code people 
> were proposing for returning null it struck me as completely the 
> wrong way to do anything.
> 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.
> 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.

This may or may not be true of Java programmers in general, is probably
true of students in general, and is probably not true of the contributors
to this list in general.   In any case, it's an ad hominem argument
that doesn't address the points that several people have made for not 
throwing an exception in this particular case.

To repeat - the two reasons for not throwing an exception are -

- Readability
- Efficiency
> Throwing the exception is the right thing to do. 

Throwing the exception is the wrong thing to do.  This isn't my
opinion -

"We used the phrase "unexpected error condition" at the beginning
of this chapter when describing when to throw exceptions.  Exceptions
are not meant for simple, expected situations."

-- The Java Programming Language - Section 7.5 - Arnold & Gosling

> 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. 

Readability and style are at least as important reasons.
Again, not my opinion -

"Many programmers do not test for all possible error conditions,
and for good reason: code becomes unintelligible if each method
invocation checks for all possible errors before the next is
executed.  This trade-off creates a tension between correctness
(checking for all errors) and clarity (not cluttering the basic
flow of code with many error checks)."

- The Java Programming Language, Chapter 7 - Arnold and Gosling

A & G give compile-time checking as the second reason.

Throwing an exception in a non-error situation (such as this one)
is the antithesis - it clutters the basic flow of code, as
Patrick Dowler's example showed.

Section 7.5 of "The Java Programming Language" also gives an
example of unnecessary checked exceptions decreasing 

> 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.

This is arguing that the whole idea of a RuntimeException, indicating
programmer error, is mistaken. 

	Steve Odendahl
	(Not speaking for Sun Microsystems, Inc.)  

> +-----------------------+------------------------+-------------------+
> | Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
> +-----------------------+------------------------+-------------------+
> |                  The XML Bible (IDG Books, 1999)                   |
> |              http://metalab.unc.edu/xml/books/bible/               |
> |   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
> +----------------------------------+---------------------------------+
> |  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
> |  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |
> +----------------------------------+---------------------------------+
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

More information about the jdom-interest mailing list