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

Jason Hunter jhunter at collab.net
Tue Jun 27 18:00:32 PDT 2000

> >Makes me wonder why we don't have getAttributeValue() though.
> Oops, sorry about that. The first JDOM source I grabbed a while back did
> have getAttributeValue(), but it apparently didn't make it into beta 4 so
> I added it back. Seems like it would make sense to include it...

Anyone remember the reason it was removed?

> Right, if the attribute is required then its absence is an error and
> throwing an exception is probably best, but if the attribute is optional
> than it's not. I'd still prefer to err on the side of simplicity and
> return null. It's possible that the absence of a key in a Hashtable
> signifies an error in the program, but I wouldn't want Hashtable's get()
> method to declare an exception based on that possibility.

Another issue is that it's common to want to get the attribute as an
int.  Right now you can do that fairly easily:

int foo = e.getAttribute("foo").getIntValue();

And check for a nice exception in case the attribute isn't there.  If
getAttribute() returns null, then calls like above will have to be
broken into multiple lines or you'll need to check for a NPE.  Now, we
could add a whole slew more getAttributeIntValue() kind of methods, but
that's an awful lot of methods, especially considering you need

getAttributeIntValue(String name)
getAttributeIntValue(String name, Namespace ns)
and maybe
getAttributeIntValue(String name, String uri)
to deal with attribute namespaces.

On the other hand, having that slew of convenience methods would allow

getAttributeIntValue("foo", 5)

where you specify a default that you can't really do if
getAttribute("foo") may throw an exception.  The combinatorial explosion
is pretty hairy though.


More information about the jdom-interest mailing list