[jdom-interest] Re: NoSuch*Exception

Alex Chaffee guru at edamame.stinky.com
Wed Jul 5 01:08:06 PDT 2000

[Disclaimer: I'm a null-head at present, but open to the exceptional
point of view.]

For the record, I subscribe to Joe Bowbeer's position: "In a large
number of situations, the absence of an attribute or element is not an
exceptional circumstance, and JDOM's accessor methods are in no
position to arbitrate."

A question for the exception-heads:

What's the difference between

try {
  value = element.getChild("foo").getChild("bar").getContent();
catch (NoSuchElementException e) {


try {
  value = element.getChild("foo").getChild("bar").getContent();
catch (NullPointerException e) {

?  The former appears slightly cleaner, but in practice it works out
the same.  Only difference is, we're not forced to catch a NPE by the
compiler.  This can be both good and bad, as others have argued.

If getChild() et al. returned null, then we could use either catch-NPE
or compare-to-null, depending on personal coding style.  If getChild()
throws an exception, we would be forced to use catch-style
programming, which is inadequate for certain cases, like this code I
just wrote last night:

    Element item = (Element) i.next();
    String title=null, url=null, date=null, location=null, description=null;
    try { title = item.getChild("title").getContent(); }
    catch (NoSuchElementException e) {}
    try { url = item.getChild("url").getContent(); }
    catch (NoSuchElementException e) {}
    try { date = item.getChild("date").getContent(); }
    catch (NoSuchElementException e) {}
    try { location = item.getChild("location").getContent(); }
    catch (NoSuchElementException e) {}
    try { description = item.getChild("description").getContent(); }
    catch (NoSuchElementException e) {}

It's really ugly catching and discarding countless exceptions in a row
like that.

If they returned null, I could do something like

Element child;
title = ((child = item.getChild("title")) == null ? null : child.getContent());
url = ((child = item.getChild("url")) == null ? null : child.getContent());

or just

title = item.getChildContent("url");

if that existed.

Furthermore, there needs to be a hasChild("foo") method to allow
pre-fetch testing, which is desirable in either the catch or compare
scenarii.  In fact, with hasChild(), even if we stayed in
exception-land, I could rewrite the above

try {
   if (item.hasChild("title"))
       title = item.getChild("title").getContent();   
   if (item.hasChild("url"))
       url = item.getChild("url").getContent();   
   if (item.hasChild("date"))
       date = item.getChild("date").getContent();
catch (NoSuchElementException e) {}

which is much cleaner than either of the others.  (It also points out
that I know for a fact that NSEE can be ignored here.)
 - Alex

P.S. I think I'm just waiting for XPath.  

P.P.S. Except I know XPath will have the same issues :-)

Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/

More information about the jdom-interest mailing list