[jdom-interest] NoSuch*Exceptions in JDOM

Patrick Dowler Patrick.Dowler at nrc.ca
Fri Jun 16 11:49:50 PDT 2000


On Fri, 16 Jun 2000, you wrote:
> > 
> > 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
> > 	}
> > 
> 
> This calls is ambiguous: was there a bug in the code, or
> did getChild return a null?

That was kind of the point - and I didn't say I liked it. It does look
almost the same as a previous example, though. Using an exception to signal 
that a child was not found is counter-intuitive. Exceptions are for error
conditions. Not finding a child element is not an error!!

BTW, if getXXX throws NoSuchElementException, you can't tell which one
threw, so you still don't know what happened. Thus, the idea that chaining
getXXXs together somehow justifies using an exception rather than returning
null is bogus, IMO. It doesn't tell  you enough unless you wrap each get call
with a try/catch... very tedious indeed. 
-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list