[jdom-interest] NoSuch*Exceptions in JDOM

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

> Would the JavaBean convention forbid "mustGetChild"
> even if there is already a "getChild"?
> How about:
>   getMandatoryChild - may throw NoSuch*Exception
> or
> getOptionalChild - may return null

This approach is starting to have a small amount of appeal to me. 
Instead of choosing to install "vi" or "emacs" maybe we should install

I tend toward getOptionalChild() because allowing a null return is
risking an NPE, and so the longer call makes it less likely you'll do
getOptionalChild("foo").getContent() without thinking better of it.

The serious problem with this approach is that people are going to think
getOptionalChild() is the call you have to use if the child's optional,
and not realize they're just two different accessors with different
error handling conditions.  Maybe this could be fixed with a better
name, but people have been throwing out names for weeks and nothing's
jumped at me.

Hmm...  I'm pondering the approach of passing a second argument in case
the element isn't found.  It's not pretty.  You get things like
getChild("foo", null) that makes it look like you have a null namespace
or something.  Its advantages are that it follows the standard Java
pattern for returning a default if something isn't located, and it would
allow some advanced uses, for example getChild("foo",
default).getContent() could return the content of the default element if
"foo" wasn't found.  A serious disadvantage is that it gives an
ambiguity error because getChild(String, Namespace) and getChild(String,
Element) both match the getChild("foo", null) call.


More information about the jdom-interest mailing list