Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4633018

Exceptions thrown by SAX handlers are not fully reported

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P5 P5
    • None
    • 1.4.1
    • xml
    • 1.1
    • x86
    • windows_nt

      Name: gm110360 Date: 02/04/2002


      FULL PRODUCT VERSION :
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Windows NT Version 4.0 with SP6a



      A DESCRIPTION OF THE PROBLEM :
      If the handler that the SAX XML Parser is using throws an
      Exception in one of the methods called from the SAX parser,
      then that exception is caught and wrapped in a new
      SaxException.

      The printStackTrace method of the SaxException does not
      print the stack trace of the inner exception it contains,
      thereby obscuring the fact that the inner exception
      even occurred at all. The SaxException does have a
      getException() method to retrieve the inner exception, but a
      casual user of this class may not notice this.

      So could SaxException be amended to recursively display
      or return the inner exception details for these methods:

      getLocalizedMessage()
      getMessage()
      printStackTrace()
      printStackTrace(PrintStream s)
      printStackTrace(PrintWriter s)
      toString()


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a class that extends
      org.xml.sax.helpers.DefaultHandler

      2. Put a try catch block around the code that runs the
      parser that catches an Exception and does a
      printStackTrace() on the exception.

      3. Parse an XML file

      4. In one of the DefaultHandler methods, say startElement()
      throw a NullPointerException

      5. The resulting stack trace printed does not include a
      reference to the location of the throw.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected:
      A stack trace that gives the location of the originating
      source statement.

      Actual:
      A stack trace that gives the location of the place where
      the original exception is caught and a SaxException is being
      thrown that wraps the original.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.NullPointerException:
              at
      org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:987)
              at javax.xml.parsers.SAXParser.parse(Unknown Source)
              at javax.xml.parsers.SAXParser.parse(Unknown Source)
              at test.quest.pakku.xml.TestCreator.main(TestCreator.java:43)

      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import org.xml.sax.Locator;
      import org.xml.sax.Attributes;
      import javax.xml.parsers.SAXParser;
      import javax.xml.parsers.SAXParserFactory;
      import org.xml.sax.helpers.DefaultHandler;

      public class TestX extends DefaultHandler
      {
          public static void main(String[] pCommandLineParameters)
          {
              try
              {
                  SAXParserFactory factory = SAXParserFactory.newInstance();
                  factory.setValidating(false);
                  SAXParser saxParser = factory.newSAXParser();
                  saxParser.parse(new File(pCommandLineParameters[0]), new TestX());
              }
              catch (Exception exception)
              {
                  exception.printStackTrace();
              }
          }

          public void startDocument()
          {
              System.out.println("startDocument()");
          }

          public void startElement(String pNamespaceURI, String pLocalName, String
      pQualifiedName, Attributes pAttributes)
          {
              if (1 == 1)
                  throw new NullPointerException("its null");

          }

          public void characters(char[] pCharacters, int pStart, int pLength)
          {
              System.out.println("characters()");
          }

          public void endElement(String pNamespaceURI, String pLocalName, String
      pQualifiedName)
          {
              System.out.println("endElement()");
          }

          public void ignorableWhitespace(char[] ch, int start, int length)
          {
              System.out.println("ignorableWhitespace()");

          }

          public void processingInstruction(String target, String data)
          {
              System.out.println("processingInstruction()");

          }

          public void setDocumentLocator(Locator locator)
          {
              System.out.println("setDocumentLocator()");
          }

          public void skippedEntity(String name)
          {
              System.out.println("skippedEntity()");
          }

          public void startPrefixMapping(String prefix, String uri)
          {
              System.out.println("startPrefixMapping()");
          }


          public void endPrefixMapping(String prefix)
          {
              System.out.println("endPrefixMapping()");
          }

          public void endDocument()
          {
              System.out.println("endDocument()");
          }
      }
      ---------- END SOURCE ----------
      (Review ID: 138731)
      ======================================================================

            nbajajsunw Neeraj Bajaj (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: