[jdom-interest] IOExceptions in build methods

Elliotte Rusty Harold elharo at metalab.unc.edu
Tue Apr 9 16:05:21 PDT 2002

The various build methods in SAXBuilder are declared to only throw 
JDOMException. e.g. :

public Document <http://www.jdom.org/docs/apidocs/org/jdom/Document.html> *build*(java.io.InputStream in) throws JDOMException <http://www.jdom.org/docs/apidocs/org/jdom/JDOMException.html>

They are catching the exceptions thrown by the underlying  XMLReader and 
converitng them all to JDOMExceptions. I'm not sure this is always 
correct. I agree that SAXExceptions should be converted to 
JDOMExceptions. However, it's also masking IOExceptions. That's what I 
don't think it shoudl be doing. JDOMException should be limited to 
genuinely XML-related problems. I don't see why we should be reporting 
an I/O erorr such as a broken socket or a missing file as anything other 
than an IOException. I can easily imagine that a program might want to 
treat I/O errors and well-fomredness errors very differently.

Yes, you could probably retrieve the original IOException from the 
getInitcause() but that's way more trouble than it should be. It just 
really feels to me like the proper semantics is that an I/O error should 
throw an IOException.The SAX XMLReader methods we rely on do distinguish 
between SAXExceptions and IOExceptions and report them separately:

public void *parse*(String <http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html> systemId)
           throws IOException <http://java.sun.com/j2se/1.4/docs/api/java/io/IOException.html>,
                  SAXException <http://java.sun.com/j2se/1.4/docs/api/org/xml/sax/SAXException.html>

Some of the build() methods in DOMBuilder have the same problem, but 
they're deprecated and going away anyway, so I'm not too worried about them.


| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|           The XML Bible, 2nd Edition (IDG Books, 2001)             |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:   http://www.cafeaulait.org/     | 
|  Read Cafe con Leche for XML News:  http://www.cafeconleche.org/   |

More information about the jdom-interest mailing list