1 /*
   2  * Copyright (c) 2003, 2015, 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 package javax.xml.bind;
  27 
  28 import java.io.PrintWriter;
  29 
  30 /**
  31  * This is the root exception class for all JAXB exceptions.
  32  *
  33  * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
  34  * @see JAXBContext
  35  * @see Marshaller
  36  * @see Unmarshaller
  37  * @since 1.6, JAXB 1.0
  38  */
  39 public class JAXBException extends Exception {
  40 
  41     /**
  42      * Vendor specific error code
  43      *
  44      */
  45     private String errorCode;
  46 
  47     /**
  48      * Exception reference
  49      *
  50      */
  51     private volatile Throwable linkedException;
  52 
  53     static final long serialVersionUID = -5621384651494307979L;
  54 
  55     /**
  56      * Construct a JAXBException with the specified detail message.  The
  57      * errorCode and linkedException will default to null.
  58      *
  59      * @param message a description of the exception
  60      */
  61     public JAXBException(String message) {
  62         this( message, null, null );
  63     }
  64 
  65     /**
  66      * Construct a JAXBException with the specified detail message and vendor
  67      * specific errorCode.  The linkedException will default to null.
  68      *
  69      * @param message a description of the exception
  70      * @param errorCode a string specifying the vendor specific error code
  71      */
  72     public JAXBException(String message, String errorCode) {
  73         this( message, errorCode, null );
  74     }
  75 
  76     /**
  77      * Construct a JAXBException with a linkedException.  The detail message and
  78      * vendor specific errorCode will default to null.
  79      *
  80      * @param exception the linked exception
  81      */
  82     public JAXBException(Throwable exception) {
  83         this( null, null, exception );
  84     }
  85 
  86     /**
  87      * Construct a JAXBException with the specified detail message and
  88      * linkedException.  The errorCode will default to null.
  89      *
  90      * @param message a description of the exception
  91      * @param exception the linked exception
  92      */
  93     public JAXBException(String message, Throwable exception) {
  94         this( message, null, exception );
  95     }
  96 
  97     /**
  98      * Construct a JAXBException with the specified detail message, vendor
  99      * specific errorCode, and linkedException.
 100      *
 101      * @param message a description of the exception
 102      * @param errorCode a string specifying the vendor specific error code
 103      * @param exception the linked exception
 104      */
 105     public JAXBException(String message, String errorCode, Throwable exception) {
 106         super( message );
 107         this.errorCode = errorCode;
 108         this.linkedException = exception;
 109     }
 110 
 111     /**
 112      * Get the vendor specific error code
 113      *
 114      * @return a string specifying the vendor specific error code
 115      */
 116     public String getErrorCode() {
 117         return this.errorCode;
 118     }
 119 
 120     /**
 121      * Get the linked exception
 122      *
 123      * @return the linked Exception, null if none exists
 124      */
 125     public Throwable getLinkedException() {
 126         return linkedException;
 127     }
 128 
 129     /**
 130      * Add a linked Exception.
 131      *
 132      * @param exception the linked Exception (A null value is permitted and
 133      *                  indicates that the linked exception does not exist or
 134      *                  is unknown).
 135      */
 136     public void setLinkedException( Throwable exception ) {
 137         this.linkedException = exception;
 138     }
 139 
 140     /**
 141      * Returns a short description of this JAXBException.
 142      *
 143      */
 144     public String toString() {
 145         return linkedException == null ?
 146             super.toString() :
 147             super.toString() + "\n - with linked exception:\n[" +
 148                                 linkedException.toString()+ "]";
 149     }
 150 
 151     /**
 152      * Prints this JAXBException and its stack trace (including the stack trace
 153      * of the linkedException if it is non-null) to the PrintStream.
 154      *
 155      * @param s PrintStream to use for output
 156      */
 157     public void printStackTrace( java.io.PrintStream s ) {
 158         super.printStackTrace(s);
 159     }
 160 
 161     /**
 162      * Prints this JAXBException and its stack trace (including the stack trace
 163      * of the linkedException if it is non-null) to {@code System.err}.
 164      *
 165      */
 166     public void printStackTrace() {
 167         super.printStackTrace();
 168     }
 169 
 170     /**
 171      * Prints this JAXBException and its stack trace (including the stack trace
 172      * of the linkedException if it is non-null) to the PrintWriter.
 173      *
 174      * @param s PrintWriter to use for output
 175      */
 176     public void printStackTrace(PrintWriter s) {
 177         super.printStackTrace(s);
 178     }
 179 
 180     @Override
 181     public Throwable getCause() {
 182         return linkedException;
 183     }
 184 }