Fwd: Re: [jdom-interest] NoSuch*Exceptions in JDOM
Jason Hunter
jhunter at collab.net
Tue Jun 27 16:20:52 PDT 2000
I'm going to keep my mouth shut on the null vs exception issue for now
as I'm still forming an opinion, but I feel compelled to clean up the
following code.
> // returning null
> String value = element.getAttributeValue("foo");
> if (value!=null) {
> // something
> }
> else {
> // something else
> }
Since there's no getAttributeValue() this really should be:
Attribute attrib = element.getAttribute("foo");
if (attrib != null) {
handleValue(attrib.getValue());
}
else {
handleNoValue();
}
Makes me wonder why we don't have getAttributeValue() though.
Meanwhile, the exception code needs changes too.
> // throwing exception
> String value=null;
> try {
> value = element.getAttributeValue("foo");
> }
> catch(NoSuchAttributeException ex) {}
> if (value!=null) {
> // something
> }
> else {
> // something else
> }
try {
handleValue(element.getAttribute("foo").getValue());
}
catch (NoSuchAttributeException e) {
handleNoValue();
}
With the current set of methods, exceptions actually seem to allow for
simpler code. Using exceptions also allows you to read an arbitrary
number of attributes which are all required or implied by the DTD, with
only one sanity-checking catch clause. With a null return we'd all be
too lazy to do n-many sanity checking null return checks, and in some
cases that'll result in nasty NPEs.
-jh-
More information about the jdom-interest
mailing list