001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang.exception;
018
019 import java.io.PrintStream;
020 import java.io.PrintWriter;
021
022 /**
023 * The base class of all runtime exceptions which can contain other
024 * exceptions.
025 *
026 * @see org.apache.commons.lang.exception.NestableException
027 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
028 * @author Daniel L. Rall
029 * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
030 * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
031 * @since 1.0
032 * @version $Id: NestableRuntimeException.java 512889 2007-02-28 18:18:20Z dlr $
033 */
034 public class NestableRuntimeException extends RuntimeException implements Nestable {
035
036 /**
037 * Required for serialization support.
038 *
039 * @see java.io.Serializable
040 */
041 private static final long serialVersionUID = 1L;
042
043 /**
044 * The helper instance which contains much of the code which we
045 * delegate to.
046 */
047 protected NestableDelegate delegate = new NestableDelegate(this);
048
049 /**
050 * Holds the reference to the exception or error that caused
051 * this exception to be thrown.
052 */
053 private Throwable cause = null;
054
055 /**
056 * Constructs a new <code>NestableRuntimeException</code> without specified
057 * detail message.
058 */
059 public NestableRuntimeException() {
060 super();
061 }
062
063 /**
064 * Constructs a new <code>NestableRuntimeException</code> with specified
065 * detail message.
066 *
067 * @param msg the error message
068 */
069 public NestableRuntimeException(String msg) {
070 super(msg);
071 }
072
073 /**
074 * Constructs a new <code>NestableRuntimeException</code> with specified
075 * nested <code>Throwable</code>.
076 *
077 * @param cause the exception or error that caused this exception to be
078 * thrown
079 */
080 public NestableRuntimeException(Throwable cause) {
081 super();
082 this.cause = cause;
083 }
084
085 /**
086 * Constructs a new <code>NestableRuntimeException</code> with specified
087 * detail message and nested <code>Throwable</code>.
088 *
089 * @param msg the error message
090 * @param cause the exception or error that caused this exception to be
091 * thrown
092 */
093 public NestableRuntimeException(String msg, Throwable cause) {
094 super(msg);
095 this.cause = cause;
096 }
097
098 /**
099 * {@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 }