[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

SAX: Exception Handling

  • From: David Megginson <ak117@f...>
  • To: "XML Developers' List" <xml-dev@i...>
  • Date: Mon, 4 May 1998 08:53:15 -0400

sax error handling
James Clark writes:

 > With my application writer's hat on (I just converted XMLTest to the new
 > SAX), the current solution is a significant step backwards.  The typical
 > simple SAX program that processes an XML document and produces some sort
 > of output is going to have to make calls in its DocumentHandler
 > implementation that throw IOExceptions, and each of these methods now
 > have to do:
 > 
 > try {
 >   ...
 > }
 > catch (IOException e) {
 >   throw new SAXException(e);
 > }
 >
 > The original goal of SAX was to be simple and easy to use for
 > application writers.  I don't think requiring this sort of mumbo jumbo
 > even for trivial programs is consistent with this goal.

I stood by the same position for nearly five months, in the face of
very loud opposition from people whose opinions I have great reason to
respect (as I have yours).

However, upon reflection (or perhaps simply mental exhaustion), I
realised that throwing java.lang.Exception was a false economy.  When
I write

 public void endElement (String name)
   throws java.lang.Exception
 {
   [..]
 }

The Java compiler will not help me discover which constructors or
methods invoked in the body can throw exceptions that I have not yet
considered.  With

 public void endElement (String name)
   throws org.xml.sax.SAXException
 {
   [..]
 }

The Java compiler will raise an error the first time I compile.  I
will discover that java.sql.Connection.createStatement (for example)
can throw a SQLException, and I will take steps either to deal with
the exception here in the handler (perhaps by reformulating the query)
or for packing it up to send it to the top level.  Of course, I can
always avoid doing so by wrapping the entire contents of each handler
in a try...catch statement

  public void endElement (String name)
    throws org.xml.sax.SAXException
  {
    try {
      [..]
    } catch (SAXException e) {
      throw e;
    } catch (Exception e) {
      throw new SAXException(e);
    }
  }

By doing so, however, I have consciously pulled the pin out of the
grenade myself, rather than having the grenade handed to me with the
pin already missing.

 > I don't see an ideal solution, but I can think of several possibilities
 > in addition to the old solution and the current solution, any of which I
 > think would be an improvement over the current solution:
 > 
 > 1.  The handler methods are declared to throw both SAXException and
 > IOException (as I proposed for parse).  My guess is that throwing
 > IOException is going to be very common, and will avoid the need for the
 > user to wrap exceptions themselves in a large proportion of simple
 > programs.  I can't see any disadvantages over the current proposal.

Your guess supposes that one class of XML application dominates.
Database applications (and they may come to dominate XML) will more
typically need to throw java.sql.SQLException, while GUI applications
will often need to throw java.awt.AWTException.

 > 2.  A variation on this would be to make SAXException extend IOException
 > and then declare both the handler methods and parse as throwing
 > IOException.

Again, this variation benefits only one group of users.  If
SQLException were a subclass of IOException (for example), then the
choice might be clearer.


All the best,


David

-- 
David Megginson                 ak117@f...
Microstar Software Ltd.         dmeggins@m...
      http://home.sprynet.com/sprynet/dmeggins/

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@i...
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo@i... the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@i... the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@i...)


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.