001 /*
002 * $Id: JAXBException.java,v 1.7 2002/11/11 18:58:20 ryans Exp $
003 *
004 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
005 *
006 * This software is the proprietary information of Sun Microsystems, Inc.
007 * Use is subject to license terms.
008 *
009 */
010
011 package javax.xml.bind;
012
013 import java.io.PrintWriter;
014
015 /**
016 * This is the root exception class for all JAXB exceptions.
017 *
018 * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
019 * @version $Revision: 1.7 $ $Date: 2002/11/11 18:58:20 $
020 * @see JAXBContext
021 * @see Marshaller
022 * @see Unmarshaller
023 * @since JAXB1.0
024 */
025 public class JAXBException extends Exception {
026
027 /**
028 * Vendor specific error code
029 *
030 */
031 private String errorCode;
032
033 /**
034 * Exception reference
035 *
036 */
037 private Throwable linkedException;
038
039
040 /**
041 * Construct a JAXBException with the specified detail message. The
042 * errorCode and linkedException will default to null.
043 *
044 * @param message a description of the exception
045 */
046 public JAXBException(String message) {
047 this( message, null, null );
048 }
049
050 /**
051 * Construct a JAXBException with the specified detail message and vendor
052 * specific errorCode. The linkedException will default to null.
053 *
054 * @param message a description of the exception
055 * @param errorCode a string specifying the vendor specific error code
056 */
057 public JAXBException(String message, String errorCode) {
058 this( message, errorCode, null );
059 }
060
061 /**
062 * Construct a JAXBException with a linkedException. The detail message and
063 * vendor specific errorCode will default to null.
064 *
065 * @param exception the linked exception
066 */
067 public JAXBException(Throwable exception) {
068 this( null, null, exception );
069 }
070
071 /**
072 * Construct a JAXBException with the specified detail message and
073 * linkedException. The errorCode will default to null.
074 *
075 * @param message a description of the exception
076 * @param exception the linked exception
077 */
078 public JAXBException(String message, Throwable exception) {
079 this( message, null, exception );
080 }
081
082 /**
083 * Construct a JAXBException with the specified detail message, vendor
084 * specific errorCode, and linkedException.
085 *
086 * @param message a description of the exception
087 * @param errorCode a string specifying the vendor specific error code
088 * @param exception the linked exception
089 */
090 public JAXBException(String message, String errorCode, Throwable exception) {
091 super( message );
092 this.errorCode = errorCode;
093 this.linkedException = exception;
094 }
095
096 /**
097 * Get the vendor specific error code
098 *
099 * @return a string specifying the vendor specific error code
100 */
101 public String getErrorCode() {
102 return this.errorCode;
103 }
104
105 /**
106 * Get the linked exception
107 *
108 * @return the linked Exception, null if none exists
109 */
110 public Throwable getLinkedException() {
111 return linkedException;
112 }
113
114 /**
115 * Add a linked Exception.
116 *
117 * @param exception the linked Exception (A null value is permitted and
118 * indicates that the linked exception does not exist or
119 * is unknown).
120 */
121 public synchronized void setLinkedException( Throwable exception ) {
122 this.linkedException = exception;
123 }
124
125 /**
126 * Returns a short description of this JAXBException.
127 *
128 */
129 public String toString() {
130 return linkedException == null ?
131 super.toString() :
132 super.toString() + "\n - with linked exception:\n[" +
133 linkedException.toString()+ "]";
134 }
135
136 /**
137 * Prints this JAXBException and its stack trace (including the stack trace
138 * of the linkedException if it is non-null) to the PrintStream.
139 *
140 * @param s PrintStream to use for output
141 */
142 public void printStackTrace( java.io.PrintStream s ) {
143 if( linkedException != null ) {
144 linkedException.printStackTrace(s);
145 s.println("--------------- linked to ------------------");
146 }
147
148 super.printStackTrace(s);
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 <tt>System.err</tt>.
154 *
155 */
156 public void printStackTrace() {
157 printStackTrace(System.err);
158 }
159
160 /**
161 * Prints this JAXBException and its stack trace (including the stack trace
162 * of the linkedException if it is non-null) to the PrintWriter.
163 *
164 * @param s PrintWriter to use for output
165 */
166 public void printStackTrace(PrintWriter s) {
167 if( linkedException != null ) {
168 linkedException.printStackTrace(s);
169 s.println("--------------- linked to ------------------");
170 }
171
172 super.printStackTrace(s);
173 }
174
175 }