Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java SAX: fatalError() writing to standard error, even though the exception is being caught.  RSS feed

 
F Turner
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using Java SAX to parse an XML file, and am implementing some error handling for if XML 'fatal errors' occur (as defined in the XML specification). All of my parsing takes place within a try - catch block, which does catch fatal errors when they occur. However, an error message is written to the standard error stream anyway, before the program continues. The default behaviour for handling fatal errors is defined in the org.xml.sax.helpers.DefaultHandler class, and can be overridden, but overriding it to exactly the same thing (just rethrowing the error passed into the handler) stops the error message from being written to standard error.

Would anybody be able to tell me why this happens, and where in the java source code I could find it?

Thanks very much :)
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

An SSCCE showing the problem would help.

I suspect the the DefaultHandler's fatal error handler writes the message to stderr. By overriding the method you prevent the DefaultHandler's method from executing and so you don't see the error message. If I'm correct and you add super.fatalError(e); to the beginning of your overriding method you should see the stderr message again.
 
F Turner
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony, and thanks!

Unfortunately that doesn't produce the message (my override was exactly what was implemented in DefaultHandler, so I shouldn't be surprised I suppose). I only get the error message if I don't assign an error handler (i.e. I don't do XMLReader.setErrorHandler(some_handler)), but the error is still caught. A code example:



Output:
[Fatal Error] :1:19: The element type "fail" must be terminated by the matching end-tag "</fail>".
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 19; The element type "fail" must be terminated by the matching end-tag "</fail>".

The error message appears twice: once through stderr, and once via the call to System.out.println().
But with the line removed, I only get the second.
 
F Turner
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I have just found this in the XMLReader documentation for XMLReader.setErrorHandler:


Allow an application to register an error event handler.

If the application does not register an error handler, all error events reported by the SAX parser will be silently ignored; however, normal processing may not continue. It is highly recommended that all SAX applications implement an error handler to avoid unexpected bugs.

Applications may register a new or different handler in the middle of a parse, and the SAX parser must begin using the new handler immediately.


Therefore, if I don't register an error handler, it won't default to DefaultHandler. The method which actually writes out the error when debugging seems to be: XMLDocumentScannerImpl(XMLDocumentFragmentScannerImpl).scanDocument(boolean), so I'll see if I can find the source code and where the write operation is being performed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!