[jdom-interest] NoSuch*Exceptions in JDOM
Andre Van Delft
avandelft at epo.org
Mon Jul 10 05:30:12 PDT 2000
Jason Hunter wrote:
> I'm still flexible. But let me say, I'm not swayed by the "ivory tower"
> question of "is this exceptional or not" but rather I'm swayed by use
> cases that make one approach more appealing than the other.
> Unfortunately, selecting either approach currently involves making
> certain use cases overly complicated. So, other things being equal, I'm
> currently supporting throwing exceptions because I just know returning
> null means there'll be a lot of damn NullPointerException errors,
> without any decent description of what went wrong. Sirtaj Singh Kang
> made some very compelling arguments along these lines.
>
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.
More information about the jdom-interest
mailing list