Now, try running the above code with this file as input:
<collection> &foo; <comic title="Sandman" number='62'> </collection>
The &foo; entity is unknown, and the comic element isn't closed (if it was empty, there would be a "/" before the closing ">". Why did the file get processed without complaint? Because the default code for the ErrorHandler interface does nothing, and no different implementation was provided, so the errors are silently ignored.
The ErrorRaiser class automatically raises an exception for any error; you'll usually set an instance of this class as the error handler. Otherwise, you should provide your own version of the ErrorHandler interface, and at minimum override the error() and fatalError() methods. The minimal implementation for each method can be a single line. The methods in the ErrorHandler interface-warning, error, and fatalError-are all passed a single argument, an exception instance. The exception will always be a subclass of SAXException, and calling str() on it will produce a readable error message explaining the problem.
So, to re-implement a variant of ErrorRaiser, simply define two of the three methods to raise the exception they're passed:
def error(self, exception): raise exception def fatalError(self, exception): raise exception
warning() might simply print the exception to sys.stderr and return without raising the exception. Now the same incorrect XML file will cause a traceback to be printed, with the error message ``xml.sax.saxlib.SAXException: reference to unknown entity''.