Path: blob/master/src/java.xml/share/classes/org/xml/sax/SAXException.java
40948 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package org.xml.sax;2627import java.io.IOException;28import java.io.InvalidClassException;29import java.io.ObjectInputStream;30import java.io.ObjectOutputStream;31import java.io.ObjectStreamField;3233/**34* Encapsulate a general SAX error or warning.35*36* <p>This class can contain basic error or warning information from37* either the XML parser or the application: a parser writer or38* application writer can subclass it to provide additional39* functionality. SAX handlers may throw this exception or40* any exception subclassed from it.</p>41*42* <p>If the application needs to pass through other types of43* exceptions, it must wrap those exceptions in a SAXException44* or an exception derived from a SAXException.</p>45*46* <p>If the parser or application needs to include information about a47* specific location in an XML document, it should use the48* {@link org.xml.sax.SAXParseException SAXParseException} subclass.</p>49*50* @since 1.4, SAX 1.051* @author David Megginson52* @version 2.0.1 (sax2r2)53* @see org.xml.sax.SAXParseException54*/55public class SAXException extends Exception {565758/**59* Create a new SAXException.60*/61public SAXException ()62{63super();64}656667/**68* Create a new SAXException.69*70* @param message The error or warning message.71*/72public SAXException (String message) {73super(message);74}757677/**78* Create a new SAXException wrapping an existing exception.79*80* <p>The existing exception will be embedded in the new81* one, and its message will become the default message for82* the SAXException.</p>83*84* @param e The exception to be wrapped in a SAXException.85*/86public SAXException (Exception e)87{88super(e);89}909192/**93* Create a new SAXException from an existing exception.94*95* <p>The existing exception will be embedded in the new96* one, but the new exception will have its own message.</p>97*98* @param message The detail message.99* @param e The exception to be wrapped in a SAXException.100*/101public SAXException (String message, Exception e)102{103super(message, e);104}105106107/**108* Return a detail message for this exception.109*110* <p>If there is an embedded exception, and if the SAXException111* has no detail message of its own, this method will return112* the detail message from the embedded exception.</p>113*114* @return The error or warning message.115*/116public String getMessage ()117{118String message = super.getMessage();119Throwable cause = super.getCause();120121if (message == null && cause != null) {122return cause.getMessage();123} else {124return message;125}126}127128/**129* Return the embedded exception, if any.130*131* @return The embedded exception, or null if there is none.132*/133public Exception getException ()134{135return getExceptionInternal();136}137138/**139* Return the cause of the exception140*141* @return Return the cause of the exception142*/143public Throwable getCause() {144return super.getCause();145}146147/**148* Override toString to pick up any embedded exception.149*150* @return A string representation of this exception.151*/152public String toString ()153{154Throwable exception = super.getCause();155if (exception != null) {156return super.toString() + "\n" + exception.toString();157} else {158return super.toString();159}160}161162163164//////////////////////////////////////////////////////////////////////165// Internal state.166//////////////////////////////////////////////////////////////////////167168/**169* serializable fields170*/171private static final ObjectStreamField[] serialPersistentFields = {172new ObjectStreamField( "exception", Exception.class )173};174175/**176* Writes "exception" field to the stream.177*178* @param out stream used for serialization.179* @throws IOException thrown by <code>ObjectOutputStream</code>180*/181private void writeObject(ObjectOutputStream out)182throws IOException {183ObjectOutputStream.PutField fields = out.putFields();184fields.put("exception", getExceptionInternal());185out.writeFields();186}187188/**189* Reads the "exception" field from the stream.190* And initializes the "exception" if it wasn't191* done before.192*193* @param in stream used for deserialization194* @throws IOException thrown by <code>ObjectInputStream</code>195* @throws ClassNotFoundException thrown by <code>ObjectInputStream</code>196*/197private void readObject(ObjectInputStream in)198throws IOException, ClassNotFoundException {199ObjectInputStream.GetField fields = in.readFields();200Exception exception = (Exception) fields.get("exception", null);201Throwable superCause = super.getCause();202203// if super.getCause() and 'exception' fields present then always use204// getCause() value. Otherwise, use 'exception' to initialize cause205if (superCause == null && exception != null) {206try {207super.initCause(exception);208} catch (IllegalStateException e) {209throw new InvalidClassException("Inconsistent state: two causes");210}211}212}213214// Internal method to guard against overriding of public getException215// method by SAXException subclasses216private Exception getExceptionInternal() {217Throwable cause = super.getCause();218if (cause instanceof Exception) {219return (Exception) cause;220} else {221return null;222}223}224225// Added serialVersionUID to preserve binary compatibility226static final long serialVersionUID = 583241635256073760L;227}228229// end of SAXException.java230231232