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