1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang.exception;
18
19 import java.io.PrintStream;
20 import java.io.PrintWriter;
21
22 /**
23 * The base class of all runtime exceptions which can contain other
24 * exceptions.
25 *
26 * @see org.apache.commons.lang.exception.NestableException
27 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
28 * @author Daniel L. Rall
29 * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
30 * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
31 * @since 1.0
32 * @version $Id: NestableRuntimeException.java 512889 2007-02-28 18:18:20Z dlr $
33 */
34 public class NestableRuntimeException extends RuntimeException implements Nestable {
35
36 /**
37 * Required for serialization support.
38 *
39 * @see java.io.Serializable
40 */
41 private static final long serialVersionUID = 1L;
42
43 /**
44 * The helper instance which contains much of the code which we
45 * delegate to.
46 */
47 protected NestableDelegate delegate = new NestableDelegate(this);
48
49 /**
50 * Holds the reference to the exception or error that caused
51 * this exception to be thrown.
52 */
53 private Throwable cause = null;
54
55 /**
56 * Constructs a new <code>NestableRuntimeException</code> without specified
57 * detail message.
58 */
59 public NestableRuntimeException() {
60 super();
61 }
62
63 /**
64 * Constructs a new <code>NestableRuntimeException</code> with specified
65 * detail message.
66 *
67 * @param msg the error message
68 */
69 public NestableRuntimeException(String msg) {
70 super(msg);
71 }
72
73 /**
74 * Constructs a new <code>NestableRuntimeException</code> with specified
75 * nested <code>Throwable</code>.
76 *
77 * @param cause the exception or error that caused this exception to be
78 * thrown
79 */
80 public NestableRuntimeException(Throwable cause) {
81 super();
82 this.cause = cause;
83 }
84
85 /**
86 * Constructs a new <code>NestableRuntimeException</code> with specified
87 * detail message and nested <code>Throwable</code>.
88 *
89 * @param msg the error message
90 * @param cause the exception or error that caused this exception to be
91 * thrown
92 */
93 public NestableRuntimeException(String msg, Throwable cause) {
94 super(msg);
95 this.cause = cause;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 public Throwable getCause() {
102 return cause;
103 }
104
105 /**
106 * Returns the detail message string of this throwable. If it was
107 * created with a null message, returns the following:
108 * (cause==null ? null : cause.toString()).
109 *
110 * @return String message string of the throwable
111 */
112 public String getMessage() {
113 if (super.getMessage() != null) {
114 return super.getMessage();
115 } else if (cause != null) {
116 return cause.toString();
117 } else {
118 return null;
119 }
120 }
121
122 /**
123 * {@inheritDoc}
124 */
125 public String getMessage(int index) {
126 if (index == 0) {
127 return super.getMessage();
128 }
129 return delegate.getMessage(index);
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 public String[] getMessages() {
136 return delegate.getMessages();
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 public Throwable getThrowable(int index) {
143 return delegate.getThrowable(index);
144 }
145
146 /**
147 * {@inheritDoc}
148 */
149 public int getThrowableCount() {
150 return delegate.getThrowableCount();
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 public Throwable[] getThrowables() {
157 return delegate.getThrowables();
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 public int indexOfThrowable(Class type) {
164 return delegate.indexOfThrowable(type, 0);
165 }
166
167 /**
168 * {@inheritDoc}
169 */
170 public int indexOfThrowable(Class type, int fromIndex) {
171 return delegate.indexOfThrowable(type, fromIndex);
172 }
173
174 /**
175 * {@inheritDoc}
176 */
177 public void printStackTrace() {
178 delegate.printStackTrace();
179 }
180
181 /**
182 * {@inheritDoc}
183 */
184 public void printStackTrace(PrintStream out) {
185 delegate.printStackTrace(out);
186 }
187
188 /**
189 * {@inheritDoc}
190 */
191 public void printStackTrace(PrintWriter out) {
192 delegate.printStackTrace(out);
193 }
194
195 /**
196 * {@inheritDoc}
197 */
198 public final void printPartialStackTrace(PrintWriter out) {
199 super.printStackTrace(out);
200 }
201
202 }