[jdom-interest] NoSuch*Exceptions: the definitive example(s)

Patrick Dowler Patrick.Dowler at nrc.ca
Mon Jul 10 10:07:32 PDT 2000


Ok, here is the really nasty use-case for exceptions. (Note: I just typed it
in here so it might have bugs, but you get the idea :-)

* We have seen the example of how throwing makes it really
   easy to something like:

try
{
     Element e =my.getChild("well").getChild("hidden").getChild("leaf");
     // do something with the "leaf"
} 
catch (NoSuchChildException ncex) 
{
     // which element was missing?

}

** If you actually want to be robust, you need:

Element well = null;
Element hidden = null;
Element leaf = null;

try
{
     well =my.getChild("well");
     hidden = well.getChild("hidden");
     leaf = hidden.getChild("leaf");
     // do something with the "leaf"
} 
catch (NoSuchChildException ncex) 
{
     if ( well == null )
	// handle missing "well"
     if ( hidden == null )
	// handle missing "hidden"
     if (leaf == null )
	// handle missing "leaf"
}

Here, you still end up comparing for null to see what went wrong. You are
not going to get NullPointerException, 

*** To avoid checking nulls anyway, it gets even worse:

try
{
     well =my.getChild("well");
} 
catch (NoSuchChildException ncex) 
{
     // handle missing "well"
}
try
{
     hidden = well.getChild("hidden");
} 
catch (NoSuchChildException ncex) 
{
     // handle missing "hidden"
}
try
{
     leaf = hidden.getChild("leaf");
     // do something with the "leaf"
} 
catch (NoSuchChildException ncex) 
{
     // handle missing "leaf"
}

Note that this only works if the error-handling blocks can either fix the
problem or throw another exception. If they fall-through, you still get a
NullPointerException in the next try block :-(

The alternative when returning null (my typical style) is:

     Element well =my.getChild("well");
     if ( well == null )
	// handle missing "well" or throw an exception
     Element hidden = well.getChild("hidden");
     if ( hidden == null )
	// handle missing "hidden" or throw an exception
     Element leaf = hidden.getChild("leaf");
     if ( leaf == null )
	// handle  missing "leaf" or throw an exception
     // do something with the "leaf"

----------------------------------------

This example is the one where the programmer has to know which thing was
actually missing and deal with it. Many previous examples had a nebuluous
 
catch (NoSuchChildException ex)
{
	handleException();
}

block. That handle method was going to have to do all this stuff, so saying
that exceptions make it easier/cleaner is simply not true. To me, it looks
like they make it easier when there are none :-) They are also great if you
can't fix the problem and want to just jump back to the calling code. I don't
think that is typically the case in JDOM-using code. It certainly isn't a
style-decision that JDOM should make, IMO: notice that in the simple
"null-returning" example, the error handling may well throw an exception
if the application can't fix the problem right there.

I'm all for exception throwing in cases where the executing code can't
do anything else and has to abort.

--

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list