[jdom-interest] NoSuch*Exceptions in JDOM

philip.nelson at omniresources.com philip.nelson at omniresources.com
Mon Jul 10 10:02:45 PDT 2000

> I see the problem in your test case: you're testing for a child which
> exists in the XML. In this case the difference between the methods is
> non-existent (as your test proves).
> Declaring that something throws exceptions is not expensive. Actually
> throwing the exceptions is.

Ah, I see now why this is an issue for you.  I was thinking of the null test
for a situation that occured infrequently.  I have little doubt that if
exceptions are regularly thrown, they would incur a performance hit.  I
never pay much attention to performance with exceptions because they should
not be frequently thrown.  

In one of my first applications to use JDOM, I didn't want to throw
exceptions for missing attributes AND I wanted to use the same code for
documents that may or may not supply all the attributes.  I was able to
structure the code so that accessing the "optional" attributes was defered
until I really needed them, and so that I didn't ever have to access them
unless they were there. But that was just one app. In my case I simply add
missing attributes.  How's that for a performace hit?

I am in favor of a getOptionalChild and getOptionalAttribute methods (or
other name) possibly with a default.  The api has already added the default
value approach for all of the get<some type conversion>Attribute methods and
this addition would not break the current api and only add a few more
methods. I DO NOT want to change the current methods.  I think there are
clearly cases where exceptions should be thrown and java doesn't give use
the option of turning off an exception.  This way we get something that
everyone can use and Brett's book is still valid, not a bad thing if this
api takes off like it appears to be taking off.

Wouldn't it be great if the optional characteristic could be determined from
the DTD or schema?

More information about the jdom-interest mailing list