Error Handlers
In addition to providing the
ContentHandler
interface for handling parsing
events, SAX provides an
ErrorHandler
interface that can be
implemented to treat various error conditions that may arise during
parsing. This class works in the same manner as the document handler
we have already constructed, but only defines three callback methods.
Through these three methods, all possible error conditions are
handled and reported by SAX parsers.
Each method receives information about the error or warning that has
occurred through a
SAXParseException
.
This object holds the line number that trouble was encountered on,
the URI of the document being treated, which could be the parsed
document or an external reference within that document, and normal
exception details such as a message and a printable stack trace. In
addition, each method can throw a SAXException
.
This may seem a bit odd at first; an exception handler that throws an
exception? Keep in mind that what each handler receives is a parsing
exception. This can be a warning that should not cause the parsing
process to stop or an error that needs to be resolved for parsing to
continue; however, the callback may need to perform system I/O or
another operation that can throw an exception, and it needs to be
able to bubble this exception up the application chain. It can do
this through the SAXException
the method is
allowed to throw.
For example, consider an error handler that receives error notifications and writes those ...
Get Java and XML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.