View Javadoc

1   
2   /* ====================================================================
3    * The Apache Software License, Version 1.1
4    *
5    * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
6    * reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   * 1. Redistributions of source code must retain the above copyright
13   *    notice, this list of conditions and the following disclaimer.
14   *
15   * 2. Redistributions in binary form must reproduce the above copyright
16   *    notice, this list of conditions and the following disclaimer in
17   *    the documentation and/or other materials provided with the
18   *    distribution.
19   *
20   * 3. The end-user documentation included with the redistribution, if
21   *    any, must include the following acknowledgement:
22   *       "This product includes software developed by the
23   *        Apache Software Foundation (http://www.apache.org/)."
24   *    Alternately, this acknowledgement may appear in the software itself,
25   *    if and wherever such third-party acknowledgements normally appear.
26   *
27   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
28   *    Foundation" must not be used to endorse or promote products derived
29   *    from this software without prior written permission. For written
30   *    permission, please contact apache@apache.org.
31   *
32   * 5. Products derived from this software may not be called "Apache"
33   *    nor may "Apache" appear in their names without prior written
34   *    permission of the Apache Software Foundation.
35   *
36   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47   * SUCH DAMAGE.
48   * ====================================================================
49   *
50   * This software consists of voluntary contributions made by many
51   * individuals on behalf of the Apache Software Foundation.  For more
52   * information on the Apache Software Foundation, please see
53   * <http://www.apache.org/>.
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 }