[jdom-interest] NoSuch*Exceptions in JDOM

Elliotte Rusty Harold elharo at metalab.unc.edu
Mon Jul 10 10:24:43 PDT 2000

At 6:05 PM +0300 7/10/00, Petri Wessman wrote:
>I did a quick test just now on my Linux workstation. This is *NOT* a
>benchmark or anything, I just wrote a loop that iterates 1M * 2 times
>through a method call that returns a StringBuffer-built string based
>on a parameter (not a constant, to avoid JVM optimization
>triggering). First it does the method calls with direct value returns
>(no exceptions throws), in the second pass it throws an exception
>every time.

This isn't a good test. Most of the time an exception won't be 
thrown!  What these tests show is that at worst the exceptional case 
takes six times as long as the non-exceptional case (and normally 
closer to twice as long). However if even as many as one in ten calls 
to getChild() throw an exception, then even in the worst case you're 
still only take about 150% as long as returning null. (More like 110% 
if we take the more likely twice as slow figures). However, even this 
is deceptive because your tests didn't do anything when null was 
returned. A typical implementation would not only test for null but 
provide additional to handle the null case. I just don't see these 
numbers being bad enough to make a decision on. Premature 
optimization is the root of all evil.

I'm still torn, but I think my vote is coming down on the side of 
throwing exceptions.  However, I'm completely aghast at the thought 
of getChild() and mustGetChild(). Pick one behavior or the other but 
not both. Part of the point of JDOM is to avoid API bloat so it can 
be simple to learn and obvious to use.  If you really don't want to 
throw exceptions there are already methods in the API that will 
return a list containing only children that actually exist.

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list