[jdom-interest] [Vote] NoSuchChildException

Patrick Dowler Patrick.Dowler at nrc.ca
Wed Jul 12 15:09:57 PDT 2000


On Wed, 12 Jul 2000, you wrote:
> 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.

This one I have to disagree with: exceptions exist as a mechanism to
allow a block of code to abort and immediately force execution back up
the call stack. This is primarily useful when you have a call stack that is
deep and you do not know which layer will be responsible for handling
a particular error condition. In Java, we get the added feature that the API
can force the calling to code to be ready for certain error types. In most
cases (IOException, for example) it is an advantage to remind the programmer
that this must be handled because one can't ever ensure that such an error
won't occur. In cases where the programmer can pre-emptively ensure the error
won't occur (say by having used a validating XML parser to build the 
Document), it may or may not be the best design.

Second, no one is saying that NPE should/is uncaught. Thus, this method
doesn't have to cause exceptions - although I'm sure there will be some
anyway - NPE is a bug in the program, even when caused by faulty
input data. Any decent program  has catches NPE at some point rather than
letting the thing crash:

try
{
	doEverything();
}
catch (NullPointerException ex)
{
	if ( userAgreesToReportTheBug(ex) )
	{
		reportBug(ex);
	}
}

** Alternative resolution if the vote is a stalemate **

It would be trivial to implement "base" JDOM to return null and then
to make an "exceptional" JDOM with subclasses that threw instead of
returning null:

Element getChild(String name)
{
	Element e = super.getChild(name);
	if (e==null)
		throw new NoSuchChildException();
	return e;
}

Then people could use the version they prefer. Someone posted code
that went the other way: it was messier and you'd get the claimed
performance hit which this avoids. 

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list