4.12 SAX Exceptions

<..set SAX parser error catching..>
 try {
   SAXParserFactory factory = SAXParserFactory.newInstance();
   SAXParser saxParser = factory.newSAXParser();
   xmlReader = saxParser.getXMLReader();
 }
 <.catch parser creation errors.>
 if( xmlReader != null ){
   try {
     xmlReader.setProperty(
        "http://xml.org/sax/properties/lexical-handler",
        new MyLexicalHandler()
     );
   }
   <.catch parser configuration errors.>
   try {
      xmlReader.parse( new File(args[0]).toURL().toString() );
   }
   <.catch parsing errors.>
 }
-_-_-

<..catch parser creation errors..>
 catch (FactoryConfigurationError fce) {
    System.err.println( "can't configure parser" );
 } catch (ParserConfigurationException pce) {
    System.err.println( "can't provide parser" );
 } catch (SAXException sxe) {
   System.err.println( sxe.getMessage () );
 }
-_-_-

<..catch parser configuration errors..>
 catch (SAXNotSupportedException nse) {
   System.out.println( "parser can't set property" );
 } catch (SAXNotRecognizedException nre) {
   System.out.println( "unrecognized property" );
 }
-_-_-

<..catch parsing errors..>
 catch (SAXParseException pe) {
     System.out.println( "Error at " + pe.getSystemId()
                          + " line " + pe.getLineNumber()
                              + ": " + pe.getMessage()     );
 } catch (SAXException se) {                 // other SAX errors
     System.out.println( "ill-formed XML file" + se.getMessage());
 } catch (IOException e) {
     System.out.println( "can't find/read file" );
 } catch (Throwable t) { t.printStackTrace(); }  // other errors
-_-_-

<..SAXCatch.java..>
 import java.io.*;
 import javax.xml.parsers.*;
 import org.xml.sax.*;
 import org.xml.sax.ext.LexicalHandler;
 
 class SAXCatch {
   static public void main(String[] args) throws Exception {
     XMLReader xmlReader = null;
     <.set SAX parser error catching.>
 } }
-_-_-