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 <a href="mailto:dlr@collab.net">Daniel Rall</a>
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 491652 2007-01-01 22:01:57Z ggregory $
033: */
034: public class NestableRuntimeException extends RuntimeException
035: implements Nestable {
036:
037: /**
038: * Required for serialization support.
039: *
040: * @see java.io.Serializable
041: */
042: private static final long serialVersionUID = 1L;
043:
044: /**
045: * The helper instance which contains much of the code which we
046: * delegate to.
047: */
048: protected NestableDelegate delegate = new NestableDelegate(this );
049:
050: /**
051: * Holds the reference to the exception or error that caused
052: * this exception to be thrown.
053: */
054: private Throwable cause = null;
055:
056: /**
057: * Constructs a new <code>NestableRuntimeException</code> without specified
058: * detail message.
059: */
060: public NestableRuntimeException() {
061: super ();
062: }
063:
064: /**
065: * Constructs a new <code>NestableRuntimeException</code> with specified
066: * detail message.
067: *
068: * @param msg the error message
069: */
070: public NestableRuntimeException(String msg) {
071: super (msg);
072: }
073:
074: /**
075: * Constructs a new <code>NestableRuntimeException</code> with specified
076: * nested <code>Throwable</code>.
077: *
078: * @param cause the exception or error that caused this exception to be
079: * thrown
080: */
081: public NestableRuntimeException(Throwable cause) {
082: super ();
083: this .cause = cause;
084: }
085:
086: /**
087: * Constructs a new <code>NestableRuntimeException</code> with specified
088: * detail message and nested <code>Throwable</code>.
089: *
090: * @param msg the error message
091: * @param cause the exception or error that caused this exception to be
092: * thrown
093: */
094: public NestableRuntimeException(String msg, Throwable cause) {
095: super (msg);
096: this .cause = cause;
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public Throwable getCause() {
103: return cause;
104: }
105:
106: /**
107: * Returns the detail message string of this throwable. If it was
108: * created with a null message, returns the following:
109: * (cause==null ? null : cause.toString()).
110: *
111: * @return String message string of the throwable
112: */
113: public String getMessage() {
114: if (super .getMessage() != null) {
115: return super .getMessage();
116: } else if (cause != null) {
117: return cause.toString();
118: } else {
119: return null;
120: }
121: }
122:
123: /**
124: * {@inheritDoc}
125: */
126: public String getMessage(int index) {
127: if (index == 0) {
128: return super .getMessage();
129: }
130: return delegate.getMessage(index);
131: }
132:
133: /**
134: * {@inheritDoc}
135: */
136: public String[] getMessages() {
137: return delegate.getMessages();
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: public Throwable getThrowable(int index) {
144: return delegate.getThrowable(index);
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: public int getThrowableCount() {
151: return delegate.getThrowableCount();
152: }
153:
154: /**
155: * {@inheritDoc}
156: */
157: public Throwable[] getThrowables() {
158: return delegate.getThrowables();
159: }
160:
161: /**
162: * {@inheritDoc}
163: */
164: public int indexOfThrowable(Class type) {
165: return delegate.indexOfThrowable(type, 0);
166: }
167:
168: /**
169: * {@inheritDoc}
170: */
171: public int indexOfThrowable(Class type, int fromIndex) {
172: return delegate.indexOfThrowable(type, fromIndex);
173: }
174:
175: /**
176: * {@inheritDoc}
177: */
178: public void printStackTrace() {
179: delegate.printStackTrace();
180: }
181:
182: /**
183: * {@inheritDoc}
184: */
185: public void printStackTrace(PrintStream out) {
186: delegate.printStackTrace(out);
187: }
188:
189: /**
190: * {@inheritDoc}
191: */
192: public void printStackTrace(PrintWriter out) {
193: delegate.printStackTrace(out);
194: }
195:
196: /**
197: * {@inheritDoc}
198: */
199: public final void printPartialStackTrace(PrintWriter out) {
200: super.printStackTrace(out);
201: }
202:
203: }
|