1 /*
2 * Copyright 2007 Ralf Joachim
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * $Id: Configuration.java 6907 2007-03-28 21:24:52Z rjoachim $
17 */
18 package org.castor.core.exceptions;
19
20 import java.io.PrintStream;
21 import java.io.PrintWriter;
22
23 /**
24 * CastorRuntimeException is the superclass of all unchecked Castor exceptions that are thrown
25 * during the normal operation of the Java Virtual Machine.
26 *
27 * @version $Id: Configuration.java,v 1.8 2006/03/08 17:25:52 jens Exp $
28 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
29 * @since 1.1.3
30 */
31 public class CastorRuntimeException extends RuntimeException {
32 /** SerialVersionUID */
33 private static final long serialVersionUID = 3984585622253325513L;
34
35 /** The cause of this exception or <code>null</code> if the cause is nonexistent or unknown. */
36 private Throwable _cause = null;
37
38 /** Has the cause of this exception been initialized? */
39 private boolean _initCause = false;
40
41 /**
42 * Constructs a new Castor runtime exception without a message. The cause is not initialized
43 * but may subsequently be initialized by a call to initCause(Throwable).
44 */
45 public CastorRuntimeException() {
46 super();
47 }
48
49 /**
50 * Constructs a new Castor runtime exception with the specified detail message. The cause is
51 * not initialized but may subsequently be initialized by a call to initCause(Throwable).
52 *
53 * @param message The detail message.
54 */
55 public CastorRuntimeException(final String message) {
56 super(message);
57 }
58
59 /**
60 * Constructs a new Castor runtime exception with the specified cause and the detail message
61 * of the cause. This constructor is useful for exceptions that are wrappers for others.
62 *
63 * @param cause The cause.
64 */
65 public CastorRuntimeException(final Throwable cause) {
66 super((cause == null) ? null : cause.getMessage());
67 _cause = cause;
68 _initCause = true;
69 }
70
71 /**
72 * Constructs a new Castor runtime exception with the specified detail message and cause.
73 *
74 * @param message The detail message.
75 * @param cause The cause.
76 */
77 public CastorRuntimeException(final String message, final Throwable cause) {
78 super(message);
79 _cause = cause;
80 _initCause = true;
81 }
82
83 /**
84 * The method emulates the JDK 1.4 Throwable version of initCause() for JDKs before 1.4.
85 * <br/>
86 * {@inheritDoc}
87 */
88 public final Throwable initCause(final Throwable cause) {
89 if (cause == this) { throw new IllegalArgumentException(); }
90 if (_initCause) { throw new IllegalStateException(); }
91 _cause = cause;
92 _initCause = true;
93 return this;
94 }
95
96 /**
97 * The method emulates the JDK 1.4 Throwable version of getCause() for JDKs before 1.4.
98 * <br/>
99 * {@inheritDoc}
100 */
101 public final Throwable getCause() {
102 return _cause;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public void printStackTrace() {
109 // Print the stack trace for this exception.
110 super.printStackTrace();
111
112 if (_cause != null) {
113 System.err.print("Caused by: ");
114 _cause.printStackTrace();
115 }
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 public final void printStackTrace(final PrintStream s) {
122 // Print the stack trace for this exception.
123 super.printStackTrace(s);
124
125 if (_cause != null) {
126 s.print("Caused by: ");
127 _cause.printStackTrace(s);
128 }
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 public final void printStackTrace(final PrintWriter w) {
135 // Print the stack trace for this exception.
136 super.printStackTrace(w);
137
138 if (_cause != null) {
139 w.print("Caused by: ");
140 _cause.printStackTrace(w);
141 }
142 }
143 }