SAX: Exceptions in Java SAX Implementation

David Megginson ak117 at
Thu Jan 8 02:38:38 GMT 1998


I think that it would be a bad idea for SAX to require the target
language to support exceptions; as a result, I don't plan to require
any of the SAX interfaces to throw specific exceptions under specific
circumstances (though exceptions like or may be thrown by code outside of SAX).
I may, however, create an exception specifically for use with the
error() implementation in the base class, for languages that support

More generally, SAX users for the Java version will almost certainly
want to be able to pass their own exceptions (such as database access
exceptions) through from their event handlers to the code that started
the parser, and that means that exceptions will have to flow through
the event handlers and the SAX parser more-or-less transparently.
Does it make sense, then, simply to allow every method in the SAX/Java
interfaces to throw java.lang.Exception?

  package org.xml.sax.DocumentHandler {
    public void startDocument () throws java.lang.Exception;
    public void endDocument () throws java.lang.Exception;
    /* etc. */

The parser itself could intercept any exceptions that it knows about
specifically, and simply pass through the rest to the caller.  An
invocation of a SAX parser in Java, then, would probably look
something like this:

  try {
    parser.parse(null, "file://localhost/home/david/foo.xml");
  } catch (IOException e) {
    /* do something */
  } catch (MyException e) {
    /* do something else */
  } catch (Exception e) {
    /* do yet another thing */

We are simply passing through exceptions for languages that support
them: languages without exceptions can simply omit this facility
without damaging the functionality of SAX.

Thanks to James Clark for raising this point a week or two ago.  I
would appreciate in particular hearing from Java and other OO design
specialists on the merits (or otherwise) of this approach.

Thanks, and all the best,


David Megginson                 ak117 at
Microstar Software Ltd.         dmeggins at

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at
Archived as:
To (un)subscribe, mailto:majordomo at the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at

More information about the Xml-dev mailing list