[jdom-interest] NoSuch*Exceptions in JDOM

Jason Hunter jhunter at acm.org
Mon Jul 10 10:16:15 PDT 2000


Andre, your example shows exactly what's wrong with returning null from
getChild()!  Notice how you're calling jnl.getChild("JTL").getContent()
without doing a null check.  Well, sometime it's going to happen that
<JTL/> isn't going to be there, and your only error message is going to
be a cryptic NullPointerException.  That's the reason we chose to throw
exceptions in the first place; because Brett and I were both tired of
tracking down NPEs, especially in already released code because this is
the sort of thing that happens when the end user does something crazy to
their XML that you didn't anticipate.

-jh-

Andre Van Delft wrote:
> 
> I am developing a program that transforms XML into HTML, using JDOM and ECS.
> A typical code snippet is:
> 
>   String str = "";
>   try {
>      // DTD: jnl = jtl,sbt?,...
>      str += jnl.getChild("JTL").getContent();
>      org.jdom.Element sbt   = getChild(jnl, "SBT");
>      if (sbt != null) str += sbt.getContent();
>      ...;
>   }
>   catch (NoSuchElementException e)
>   {
>      handle (target, e);
>   }
> 
> Here getChild(jnl, "SBT") is a call to my util function
> that returns null instead of throwing an exception.
> The null test is exactly what I want for an optional element.
> The catch clause is only handling real exceptions.
> Without my util function, the code would be far more
> complicated:
> 
>   String str = "";
>   try {
>      // DTD: jnl = jtl,sbt?,...
>      str += jnl.getChild("JTL").getContent();
>      try {
>         str += jnl.getChild("SBT").getContent();
>      }
>      catch (NoSuchElementException e)
>      {
>           // nothing to do; is OK
>      }
>      ...;
>   }
>   catch (NoSuchElementException e)
>   {
>      handle (target, e);
>   }
> 
> IMHO: instead of everybody making his own util functions,
> JDOM should provide the desired function variations.
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com





More information about the jdom-interest mailing list