[jdom-interest] Re: NoSuch*Exception

Sirtaj Singh Kang ssk at physics.unimelb.EDU.AU
Wed Jun 28 12:43:58 PDT 2000

On Wed, Jun 28, 2000 at 09:38:40AM -0700, Dino Valente wrote:
> I think you missed what people have been saying. Returning null should be
> used in cases where optional elements are accessed. It appears people are

The problems here are:

-> The "optional" nature of the elements is not defined by your code but
by an external DTD, which may be outside your control and subject to change.
In the situation of other elements becoming optional after your code is
written (or it was always optional and you misread the DTD), throwing an 
exception would still allow you to catch any possible errors of this type 
in a single place.

-> The case of an incorrectly spelled element is still not addressed.

I am working on the assumption that an XML document is less like a
container than it is like file IO - it normally comes from an untrusted
source and as much error checking should be done as humanly possible.

> saying that is more convenient to check for null that using exception
> handling (i.e run-time errors would not occur in these cases; OK you have
> to make sure that you check for null). It is obvious to me we require both. 

Perhaps a boolean hasChild("foo")? It serves your purpose and still
reduces the chance of a NPE.

OTOH it requires use and lookup of the element name twice...

Sirtaj S. Kang       taj at kde.org         ssk at physics.unimelb.edu.au
Univ of Melbourne	The "gui" in "Penguin" is pronounced "K-D-E"

More information about the jdom-interest mailing list