1 /* 2 * Copyright (c) 2000, 2018, 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 jdk.internal.org.xml.sax; 32 33 import java.io.IOException; 34 import java.io.InvalidClassException; 35 import java.io.ObjectInputStream; 36 import java.io.ObjectOutputStream; 37 import java.io.ObjectStreamField; 38 39 /** 40 * Encapsulate a general SAX error or warning. 41 * 42 * <blockquote> 43 * <em>This module, both source code and documentation, is in the 44 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> 45 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> 46 * for further information. 47 * </blockquote> 48 * 49 * <p>This class can contain basic error or warning information from 50 * either the XML parser or the application: a parser writer or 51 * application writer can subclass it to provide additional 52 * functionality. SAX handlers may throw this exception or 53 * any exception subclassed from it.</p> 54 * 55 * <p>If the application needs to pass through other types of 56 * exceptions, it must wrap those exceptions in a SAXException 57 * or an exception derived from a SAXException.</p> 58 * 59 * <p>If the parser or application needs to include information about a 60 * specific location in an XML document, it should use the 61 * {@link org.xml.sax.SAXParseException SAXParseException} subclass.</p> 62 * 63 * @since SAX 1.0 64 * @author David Megginson 65 * @version 2.0.1 (sax2r2) 66 * @see org.xml.sax.SAXParseException 67 */ 68 public class SAXException extends Exception { 69 70 71 /** 72 * Create a new SAXException. 73 */ 74 public SAXException () 75 { 76 super(); 77 } 78 79 80 /** 81 * Create a new SAXException. 82 * 83 * @param message The error or warning message. 84 */ 85 public SAXException (String message) { 86 super(message); 87 } 88 89 90 /** 91 * Create a new SAXException wrapping an existing exception. 92 * 93 * <p>The existing exception will be embedded in the new 94 * one, and its message will become the default message for 95 * the SAXException.</p> 96 * 97 * @param e The exception to be wrapped in a SAXException. 98 */ 99 public SAXException (Exception e) 100 { 101 super(e); 102 } 103 104 105 /** 106 * Create a new SAXException from an existing exception. 107 * 108 * <p>The existing exception will be embedded in the new 109 * one, but the new exception will have its own message.</p> 110 * 111 * @param message The detail message. 112 * @param e The exception to be wrapped in a SAXException. 113 */ 114 public SAXException (String message, Exception e) 115 { 116 super(message, e); 117 } 118 119 120 /** 121 * Return a detail message for this exception. 122 * 123 * <p>If there is an embedded exception, and if the SAXException 124 * has no detail message of its own, this method will return 125 * the detail message from the embedded exception.</p> 126 * 127 * @return The error or warning message. 128 */ 129 public String getMessage () 130 { 131 String message = super.getMessage(); 132 Throwable cause = super.getCause(); 133 134 if (message == null && cause != null) { 135 return cause.getMessage(); 136 } else { 137 return message; 138 } 139 } 140 141 /** 142 * Return the embedded exception, if any. 143 * 144 * @return The embedded exception, or null if there is none. 145 */ 146 public Exception getException () 147 { 148 return getExceptionInternal(); 149 } 150 151 /** 152 * Return the cause of the exception 153 * 154 * @return Return the cause of the exception 155 */ 156 public Throwable getCause() { 157 return super.getCause(); 158 } 159 160 /** 161 * Override toString to pick up any embedded exception. 162 * 163 * @return A string representation of this exception. 164 */ 165 public String toString () 166 { 167 Throwable exception = super.getCause(); 168 if (exception != null) { 169 return super.toString() + "\n" + exception.toString(); 170 } else { 171 return super.toString(); 172 } 173 } 174 175 176 177 ////////////////////////////////////////////////////////////////////// 178 // Internal state. 179 ////////////////////////////////////////////////////////////////////// 180 181 @java.io.Serial 182 private static final ObjectStreamField[] serialPersistentFields = { 183 new ObjectStreamField( "exception", Exception.class ) 184 }; 185 186 /** 187 * Writes "exception" field to the stream. 188 * 189 * @param out stream used for serialization. 190 * @throws IOException thrown by <code>ObjectOutputStream</code> 191 */ 192 @java.io.Serial 193 private void writeObject(ObjectOutputStream out) 194 throws IOException { 195 ObjectOutputStream.PutField fields = out.putFields(); 196 fields.put("exception", getExceptionInternal()); 197 out.writeFields(); 198 } 199 200 /** 201 * Reads the "exception" field from the stream. 202 * And initializes the "exception" if it wasn't 203 * done before. 204 * 205 * @param in stream used for deserialization 206 * @throws IOException thrown by <code>ObjectInputStream</code> 207 * @throws ClassNotFoundException thrown by <code>ObjectInputStream</code> 208 */ 209 @java.io.Serial 210 private void readObject(ObjectInputStream in) 211 throws IOException, ClassNotFoundException { 212 ObjectInputStream.GetField fields = in.readFields(); 213 Exception exception = (Exception) fields.get("exception", null); 214 Throwable superCause = super.getCause(); 215 216 // if super.getCause() and 'exception' fields present then always use 217 // getCause() value. Otherwise, use 'exception' to initialize cause 218 if (superCause == null && exception != null) { 219 try { 220 super.initCause(exception); 221 } catch (IllegalStateException e) { 222 throw new InvalidClassException("Inconsistent state: two causes"); 223 } 224 } 225 } 226 227 // Internal method to guard against overriding of public getException 228 // method by SAXException subclasses 229 private Exception getExceptionInternal() { 230 Throwable cause = super.getCause(); 231 if (cause instanceof Exception) { 232 return (Exception) cause; 233 } else { 234 return null; 235 } 236 } 237 238 // Added serialVersionUID to preserve binary compatibility 239 @java.io.Serial 240 static final long serialVersionUID = 583241635256073760L; 241 } 242 243 // end of SAXException.java