1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package org.jdiagnose.exception;
56
57 import java.io.PrintStream;
58 import java.io.PrintWriter;
59
60 /***
61 * The base class of all exceptions which can contain other exceptions.
62 *
63 * It is intended to ease the debugging by carrying on the information
64 * about the exception which was caught and provoked throwing the
65 * current exception. Catching and rethrowing may occur multiple
66 * times, and provided that all exceptions except the first one
67 * are descendants of <code>NestedException</code>, when the
68 * exception is finally printed out using any of the <code>
69 * printStackTrace()</code> methods, the stacktrace will contain
70 * the information about all exceptions thrown and caught on
71 * the way.
72 * <p> Running the following program
73 * <p><blockquote><pre>
74 * 1 import org.jdiagnose.exception.NestableException;
75 * 2
76 * 3 public class Test {
77 * 4 public static void main( String[] args ) {
78 * 5 try {
79 * 6 a();
80 * 7 } catch(Exception e) {
81 * 8 e.printStackTrace();
82 * 9 }
83 * 10 }
84 * 11
85 * 12 public static void a() throws Exception {
86 * 13 try {
87 * 14 b();
88 * 15 } catch(Exception e) {
89 * 16 throw new NestableException("foo", e);
90 * 17 }
91 * 18 }
92 * 19
93 * 20 public static void b() throws Exception {
94 * 21 try {
95 * 22 c();
96 * 23 } catch(Exception e) {
97 * 24 throw new NestableException("bar", e);
98 * 25 }
99 * 26 }
100 * 27
101 * 28 public static void c() throws Exception {
102 * 29 throw new Exception("baz");
103 * 30 }
104 * 31 }
105 * </pre></blockquote>
106 * <p>Yields the following stacktrace:
107 * <p><blockquote><pre>
108 * org.jdiagnose.exception.NestableException: foo
109 * at Test.a(Test.java:16)
110 * at Test.main(Test.java:6)
111 * Caused by: org.jdiagnose.exception.NestableException: bar
112 * at Test.b(Test.java:24)
113 * at Test.a(Test.java:14)
114 * ... 1 more
115 * Caused by: java.lang.Exception: baz
116 * at Test.c(Test.java:29)
117 * at Test.b(Test.java:22)
118 * ... 2 more
119 * </pre></blockquote><br>
120 *
121 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
122 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
123 * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
124 * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
125 * @since 1.0
126 * @version $Id: NestableException.java,v 1.2 2005/03/02 17:07:09 dkfn Exp $
127 */
128 public class NestableException extends Exception implements Nestable {
129
130 /***
131 * The helper instance which contains much of the code which we
132 * remoteResult to.
133 */
134 protected NestableDelegate delegate = new NestableDelegate(this);
135
136 /***
137 * Holds the reference to the exception or error that caused
138 * this exception to be thrown.
139 */
140 private Throwable cause = null;
141
142 /***
143 * Constructs a new <code>NestableException</code> without specified
144 * detail message.
145 */
146 public NestableException() {
147 super();
148 }
149
150 /***
151 * Constructs a new <code>NestableException</code> with specified
152 * detail message.
153 *
154 * @param msg The error message.
155 */
156 public NestableException(String msg) {
157 super(msg);
158 }
159
160 /***
161 * Constructs a new <code>NestableException</code> with specified
162 * nested <code>Throwable</code>.
163 *
164 * @param cause the exception or error that caused this exception to be
165 * thrown
166 */
167 public NestableException(Throwable cause) {
168 super();
169 this.cause = cause;
170 }
171
172 /***
173 * Constructs a new <code>NestableException</code> with specified
174 * detail message and nested <code>Throwable</code>.
175 *
176 * @param msg the error message
177 * @param cause the exception or error that caused this exception to be
178 * thrown
179 */
180 public NestableException(String msg, Throwable cause) {
181 super(msg);
182 this.cause = cause;
183 }
184
185 public Throwable getCause() {
186 return cause;
187 }
188
189 /***
190 * Returns the detail message string of this throwable. If it was
191 * created with a null message, returns the following:
192 * (cause==null ? null : cause.toString()).
193 */
194 public String getMessage() {
195 if (super.getMessage() != null) {
196 return super.getMessage();
197 } else if (cause != null) {
198 return cause.toString();
199 } else {
200 return null;
201 }
202 }
203
204 public String getMessage(int index) {
205 if (index == 0) {
206 return super.getMessage();
207 } else {
208 return delegate.getMessage(index);
209 }
210 }
211
212 public String[] getMessages() {
213 return delegate.getMessages();
214 }
215
216 public Throwable getThrowable(int index) {
217 return delegate.getThrowable(index);
218 }
219
220 public int getThrowableCount() {
221 return delegate.getThrowableCount();
222 }
223
224 public Throwable[] getThrowables() {
225 return delegate.getThrowables();
226 }
227
228 public int indexOfThrowable(Class type) {
229 return delegate.indexOfThrowable(type, 0);
230 }
231
232 public int indexOfThrowable(Class type, int fromIndex) {
233 return delegate.indexOfThrowable(type, fromIndex);
234 }
235
236 public void printStackTrace() {
237 delegate.printStackTrace();
238 }
239
240 public void printStackTrace(PrintStream out) {
241 delegate.printStackTrace(out);
242 }
243
244 public void printStackTrace(PrintWriter out) {
245 delegate.printStackTrace(out);
246 }
247
248 public final void printPartialStackTrace(PrintWriter out) {
249 super.printStackTrace(out);
250 }
251
252 }