[jdom-interest] [Vote] NoSuchChildException

Vincent Aumont vincent.aumont at vslab.com
Wed Jul 12 15:12:29 PDT 2000


Elliotte,

This is not meant to be a flamebait.

If I understand your point, your reasoming is not about the specificities of XML
(wheter or not missing elements
are the norm rather than the exception) but about methods that return something
you did not ask for (in our case: returning null when what you asked for was an
Element).
A method named getBlah should always return a Blah and since 'null' is not a Blah, it
should
throw NoBlahException instead of returning null.

Following this line of thinking, would you say that String.indexOf(...) and
String.lastIndexOf(...)
should throw NoSuchSubstringException instead of returning -1?

-Vincent.

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