Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathException.java
40948 views
/*1* Copyright (c) 2003, 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 javax.xml.xpath;2627import java.io.PrintWriter;28import java.io.IOException;29import java.io.ObjectInputStream;30import java.io.ObjectOutputStream;31import java.io.ObjectStreamField;32import java.io.InvalidClassException;3334/**35* {@code XPathException} represents a generic XPath exception.36*37* @author Norman Walsh38* @author Jeff Suttor39* @since 1.540*/41public class XPathException extends Exception {42/**43* Serializable fields.44*/45private static final ObjectStreamField[] serialPersistentFields = {46new ObjectStreamField( "cause", Throwable.class )47};4849/**50* Stream Unique Identifier.51*/52private static final long serialVersionUID = -1837080260374986980L;5354/**55* Constructs a new {@code XPathException}56* with the specified detail {@code message}.57*58* <p>The {@code cause} is not initialized.59*60* <p>If {@code message} is {@code null},61* then a {@code NullPointerException} is thrown.62*63* @param message The detail message.64*65* @throws NullPointerException When {@code message} is66* {@code null}.67*/68public XPathException(String message) {69super(message);70if ( message == null ) {71throw new NullPointerException ( "message can't be null");72}73}7475/**76* Constructs a new {@code XPathException}77* with the specified {@code cause}.78*79* <p>If {@code cause} is {@code null},80* then a {@code NullPointerException} is thrown.81*82* @param cause The cause.83*84* @throws NullPointerException if {@code cause} is {@code null}.85*/86public XPathException(Throwable cause) {87super(cause);88if ( cause == null ) {89throw new NullPointerException ( "cause can't be null");90}91}9293/**94* Get the cause of this XPathException.95*96* @return Cause of this XPathException.97*/98public Throwable getCause() {99return super.getCause();100}101102/**103* Writes "cause" field to the stream.104* The cause is got from the parent class.105*106* @param out stream used for serialization.107* @throws IOException thrown by {@code ObjectOutputStream}108*109*/110private void writeObject(ObjectOutputStream out)111throws IOException112{113ObjectOutputStream.PutField fields = out.putFields();114fields.put("cause", super.getCause());115out.writeFields();116}117118/**119* Reads the "cause" field from the stream.120* And initializes the "cause" if it wasn't121* done before.122*123* @param in stream used for deserialization124* @throws IOException thrown by {@code ObjectInputStream}125* @throws ClassNotFoundException thrown by {@code ObjectInputStream}126*/127private void readObject(ObjectInputStream in)128throws IOException, ClassNotFoundException129{130ObjectInputStream.GetField fields = in.readFields();131Throwable scause = (Throwable) fields.get("cause", null);132133if (super.getCause() == null && scause != null) {134try {135super.initCause(scause);136} catch(IllegalStateException e) {137throw new InvalidClassException("Inconsistent state: two causes");138}139}140}141142/**143* Print stack trace to specified {@code PrintStream}.144*145* @param s Print stack trace to this {@code PrintStream}.146*/147public void printStackTrace(java.io.PrintStream s) {148if (getCause() != null) {149getCause().printStackTrace(s);150s.println("--------------- linked to ------------------");151}152153super.printStackTrace(s);154}155156/**157* Print stack trace to {@code System.err}.158*/159public void printStackTrace() {160printStackTrace(System.err);161}162163/**164* Print stack trace to specified {@code PrintWriter}.165*166* @param s Print stack trace to this {@code PrintWriter}.167*/168public void printStackTrace(PrintWriter s) {169170if (getCause() != null) {171getCause().printStackTrace(s);172s.println("--------------- linked to ------------------");173}174175super.printStackTrace(s);176}177}178179180