[jdom-interest] NoSuch*Exceptions in JDOM

Kevin Regan kevinr at valicert.com
Fri Jun 16 12:41:12 PDT 2000


Actually, the chaining together with a NoSuchElementException tells
you that the XML document was malformed, which may be enough in most
cases.  If, on the other hand, the application needs greater control
of the error, then it can look at each step in the chain (rather than
doing a chain).

The error message:

try {
	getChild...getChild...getChild...
}
catch ( NoSuchElementException e ) {
	System.err.println( "Element missing in XML document: " +
e.getMessage() );
	...
}

is extremely useful.  It tells the user exactly what is wrong
with the document.  It is most likely the case that the application
does not need to know anything other than this to do the appropriate
error handling (document is invalid, so don't process it).

Also, it is useful to do the chaining when you know that the document
is already valid.  My point is that this is far more useful than
throwing a NullPointerException through accessing a null pointer (which
may be interpreted as a bug somewhere inside getChild).

--Kevin

------------------------------------------------------------------------
------ 

That was kind of the point - and I didn't say I liked it. It does look
almost the same as a previous example, though. Using an exception to
signal 
that a child was not found is counter-intuitive. Exceptions are for
error
conditions. Not finding a child element is not an error!!

BTW, if getXXX throws NoSuchElementException, you can't tell which one
threw, so you still don't know what happened. Thus, the idea that
chaining
getXXXs together somehow justifies using an exception rather than
returning
null is bogus, IMO. It doesn't tell  you enough unless you wrap each get
call
with a try/catch... very tedious indeed. 
-- 

Patrick Dowler
Canadian Astronomy Data Centre

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@you
rhost.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 2979 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20000616/c0d2189d/smime.bin


More information about the jdom-interest mailing list