View Javadoc
1   /*
2    * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  // SAX exception class.
27  // http://www.saxproject.org
28  // No warranty; no copyright -- use this as you will.
29  // $Id: SAXException.java,v 1.3 2004/11/03 22:55:32 jsuttor Exp $
30  
31  package org.xml.sax;
32  
33  /**
34   * Encapsulate a general SAX error or warning.
35   *
36   * <blockquote>
37   * <em>This module, both source code and documentation, is in the
38   * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
39   * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>;
40   * for further information.
41   * </blockquote>
42   *
43   * <p>This class can contain basic error or warning information from
44   * either the XML parser or the application: a parser writer or
45   * application writer can subclass it to provide additional
46   * functionality.  SAX handlers may throw this exception or
47   * any exception subclassed from it.</p>
48   *
49   * <p>If the application needs to pass through other types of
50   * exceptions, it must wrap those exceptions in a SAXException
51   * or an exception derived from a SAXException.</p>
52   *
53   * <p>If the parser or application needs to include information about a
54   * specific location in an XML document, it should use the
55   * {@link org.xml.sax.SAXParseException SAXParseException} subclass.</p>
56   *
57   * @since SAX 1.0
58   * @author David Megginson
59   * @version 2.0.1 (sax2r2)
60   * @see org.xml.sax.SAXParseException
61   */
62  public class SAXException extends Exception {
63  
64  
65      /**
66       * Create a new SAXException.
67       */
68      public SAXException ()
69      {
70          super();
71          this.exception = null;
72      }
73  
74  
75      /**
76       * Create a new SAXException.
77       *
78       * @param message The error or warning message.
79       */
80      public SAXException (String message) {
81          super(message);
82          this.exception = null;
83      }
84  
85  
86      /**
87       * Create a new SAXException wrapping an existing exception.
88       *
89       * <p>The existing exception will be embedded in the new
90       * one, and its message will become the default message for
91       * the SAXException.</p>
92       *
93       * @param e The exception to be wrapped in a SAXException.
94       */
95      public SAXException (Exception e)
96      {
97          super();
98          this.exception = e;
99      }
100 
101 
102     /**
103      * Create a new SAXException from an existing exception.
104      *
105      * <p>The existing exception will be embedded in the new
106      * one, but the new exception will have its own message.</p>
107      *
108      * @param message The detail message.
109      * @param e The exception to be wrapped in a SAXException.
110      */
111     public SAXException (String message, Exception e)
112     {
113         super(message);
114         this.exception = e;
115     }
116 
117 
118     /**
119      * Return a detail message for this exception.
120      *
121      * <p>If there is an embedded exception, and if the SAXException
122      * has no detail message of its own, this method will return
123      * the detail message from the embedded exception.</p>
124      *
125      * @return The error or warning message.
126      */
127     public String getMessage ()
128     {
129         String message = super.getMessage();
130 
131         if (message == null && exception != null) {
132             return exception.getMessage();
133         } else {
134             return message;
135         }
136     }
137 
138 
139     /**
140      * Return the embedded exception, if any.
141      *
142      * @return The embedded exception, or null if there is none.
143      */
144     public Exception getException ()
145     {
146         return exception;
147     }
148 
149     /**
150      * Return the cause of the exception
151      *
152      * @return Return the cause of the exception
153      */
154     public Throwable getCause() {
155         return exception;
156     }
157 
158     /**
159      * Override toString to pick up any embedded exception.
160      *
161      * @return A string representation of this exception.
162      */
163     public String toString ()
164     {
165         if (exception != null) {
166             return super.toString() + "\n" + exception.toString();
167         } else {
168             return super.toString();
169         }
170     }
171 
172 
173 
174     //////////////////////////////////////////////////////////////////////
175     // Internal state.
176     //////////////////////////////////////////////////////////////////////
177 
178 
179     /**
180      * @serial The embedded exception if tunnelling, or null.
181      */
182     private Exception exception;
183 
184     // Added serialVersionUID to preserve binary compatibility
185     static final long serialVersionUID = 583241635256073760L;
186 }
187 
188 // end of SAXException.java