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 errors which can contain other exceptions.
24 *
25 * @author Daniel L. Rall
26 * @see org.apache.commons.lang.exception.NestableException
27 * @since 1.0
28 * @version $Id: NestableError.java 512889 2007-02-28 18:18:20Z dlr $
29 */
30 public class NestableError extends Error implements Nestable {
31
32 /**
33 * Required for serialization support.
34 *
35 * @see java.io.Serializable
36 */
37 private static final long serialVersionUID = 1L;
38
39 /**
40 * The helper instance which contains much of the code which we
41 * delegate to.
42 */
43 protected NestableDelegate delegate = new NestableDelegate(this);
44
45 /**
46 * Holds the reference to the exception or error that caused
47 * this exception to be thrown.
48 */
49 private Throwable cause = null;
50
51 /**
52 * Constructs a new <code>NestableError</code> without specified
53 * detail message.
54 */
55 public NestableError() {
56 super();
57 }
58
59 /**
60 * Constructs a new <code>NestableError</code> with specified
61 * detail message.
62 *
63 * @param msg The error message.
64 */
65 public NestableError(String msg) {
66 super(msg);
67 }
68
69 /**
70 * Constructs a new <code>NestableError</code> with specified
71 * nested <code>Throwable</code>.
72 *
73 * @param cause the exception or error that caused this exception to be
74 * thrown
75 */
76 public NestableError(Throwable cause) {
77 super();
78 this.cause = cause;
79 }
80
81 /**
82 * Constructs a new <code>NestableError</code> with specified
83 * detail message and nested <code>Throwable</code>.
84 *
85 * @param msg the error message
86 * @param cause the exception or error that caused this exception to be
87 * thrown
88 */
89 public NestableError(String msg, Throwable cause) {
90 super(msg);
91 this.cause = cause;
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 public Throwable getCause() {
98 return cause;
99 }
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 }