Fwd: Re: [jdom-interest] NoSuch*Exceptions in JDOM

Thomas M. Sasala tsasala at hifusion.com
Wed Jun 28 04:51:50 PDT 2000


Jason Hunter wrote:
> 
> 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.

	We've created getAttributeValue to eliminate
the exceptions thrown by getAttribute (since nearly all the 
time we want the value, not the actual attribute).

	-Tom

> 
> 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-
> _______________________________________________
> 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