[jdom-interest] NoSuch*Exceptions in JDOM

Patrick Dowler Patrick.Dowler at nrc.ca
Fri Jun 16 10:31:21 PDT 2000


On Fri, 16 Jun 2000, Jools Enticknap wrote:
> It's a dichotomy, for NoSuchElementException to exist in this context
> because for an element not to have a child is perfectly reasonable, and
> it's not an exceptional condition. IMOHO.

100% agreed. 

All the arguments about which design entails the most code are not particularly
relevant. You have to be able to deal with the fact that a child isn't there,
and it has been shown that every method is about the same, codewise.

However, Jools has hit the nail on the head here: the standard use of
exceptions is to signal an error (typically in usage, such as invalid input
to a method, sometimes a programming error). 

BTW, if getChild() were to return null on occasion, the people who like to
chain a bunch of getChild calls together can still do it:

	try
	{
		attr = el.getChild("foo").getChild("bar").getAttribute("id");
		// do something
	}
	catch (NullPointerException ex)
	{
		// ooops, one of the gets returned null
	}

However, people who like to check for null can do it their way too. Since
a child might not exist, you have to check in any robust code. Returning
null is more consistent with standard Java style. For style, I look to
java.util.Map.get(Object key), which returns null if the key doesn't exist.
This is the most similar operation in the collections classes.

--

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list