KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > NestableException


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

54 package org.hibernate.exception;
55
56 import java.io.PrintStream JavaDoc;
57 import java.io.PrintWriter JavaDoc;
58
59 /**
60  * The base class of all exceptions which can contain other exceptions.
61  * <p/>
62  * It is intended to ease the debugging by carrying on the information
63  * about the exception which was caught and provoked throwing the
64  * current exception. Catching and rethrowing may occur multiple
65  * times, and provided that all exceptions except the first one
66  * are descendands of <code>NestedException</code>, when the
67  * exception is finally printed out using any of the <code>
68  * printStackTrace()</code> methods, the stacktrace will contain
69  * the information about all exceptions thrown and caught on
70  * the way.
71  * <p> Running the following program
72  * <p><blockquote><pre>
73  * 1 import org.apache.commons.lang.exception.NestableException;
74  * 2
75  * 3 public class Test {
76  * 4 public static void main( String[] args ) {
77  * 5 try {
78  * 6 a();
79  * 7 } catch(Exception e) {
80  * 8 e.printStackTrace();
81  * 9 }
82  * 10 }
83  * 11
84  * 12 public static void a() throws Exception {
85  * 13 try {
86  * 14 b();
87  * 15 } catch(Exception e) {
88  * 16 throw new NestableException("foo", e);
89  * 17 }
90  * 18 }
91  * 19
92  * 20 public static void b() throws Exception {
93  * 21 try {
94  * 22 c();
95  * 23 } catch(Exception e) {
96  * 24 throw new NestableException("bar", e);
97  * 25 }
98  * 26 }
99  * 27
100  * 28 public static void c() throws Exception {
101  * 29 throw new Exception("baz");
102  * 30 }
103  * 31 }
104  * </pre></blockquote>
105  * <p>Yields the following stacktrace:
106  * <p><blockquote><pre>
107  * org.apache.commons.lang.exception.NestableException: foo
108  * at Test.a(Test.java:16)
109  * at Test.main(Test.java:6)
110  * Caused by: org.apache.commons.lang.exception.NestableException: bar
111  * at Test.b(Test.java:24)
112  * at Test.a(Test.java:14)
113  * ... 1 more
114  * Caused by: java.lang.Exception: baz
115  * at Test.c(Test.java:29)
116  * at Test.b(Test.java:22)
117  * ... 2 more
118  * </pre></blockquote><br>
119  *
120  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
121  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
122  * @author <a HREF="mailto:knielsen@apache.org">Kasper Nielsen</a>
123  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
124  * @version $Id: NestableException.java,v 1.2 2004/11/21 00:11:27 pgmjsd Exp $
125  * @since 1.0
126  */

127 public class NestableException extends Exception JavaDoc implements Nestable {
128
129     /**
130      * The helper instance which contains much of the code which we
131      * delegate to.
132      */

133     protected NestableDelegate delegate = new NestableDelegate( this );
134
135     /**
136      * Holds the reference to the exception or error that caused
137      * this exception to be thrown.
138      */

139     private Throwable JavaDoc cause = null;
140
141     /**
142      * Constructs a new <code>NestableException</code> without specified
143      * detail message.
144      */

145     public NestableException() {
146         super();
147     }
148
149     /**
150      * Constructs a new <code>NestableException</code> with specified
151      * detail message.
152      *
153      * @param msg The error message.
154      */

155     public NestableException(String JavaDoc msg) {
156         super( msg );
157     }
158
159     /**
160      * Constructs a new <code>NestableException</code> with specified
161      * nested <code>Throwable</code>.
162      *
163      * @param cause the exception or error that caused this exception to be
164      * thrown
165      */

166     public NestableException(Throwable JavaDoc cause) {
167         super();
168         this.cause = cause;
169     }
170
171     /**
172      * Constructs a new <code>NestableException</code> with specified
173      * detail message and nested <code>Throwable</code>.
174      *
175      * @param msg the error message
176      * @param cause the exception or error that caused this exception to be
177      * thrown
178      */

179     public NestableException(String JavaDoc msg, Throwable JavaDoc cause) {
180         super( msg );
181         this.cause = cause;
182     }
183
184     public Throwable JavaDoc getCause() {
185         return cause;
186     }
187
188     /**
189      * Returns the detail message string of this throwable. If it was
190      * created with a null message, returns the following:
191      * ( cause==null ? null : cause.toString() ).
192      */

193     public String JavaDoc getMessage() {
194         if ( super.getMessage() != null ) {
195             return super.getMessage();
196         }
197         else if ( cause != null ) {
198             return cause.toString();
199         }
200         else {
201             return null;
202         }
203     }
204
205     public String JavaDoc getMessage(int index) {
206         if ( index == 0 ) {
207             return super.getMessage();
208         }
209         else {
210             return delegate.getMessage( index );
211         }
212     }
213
214     public String JavaDoc[] getMessages() {
215         return delegate.getMessages();
216     }
217
218     public Throwable JavaDoc getThrowable(int index) {
219         return delegate.getThrowable( index );
220     }
221
222     public int getThrowableCount() {
223         return delegate.getThrowableCount();
224     }
225
226     public Throwable JavaDoc[] getThrowables() {
227         return delegate.getThrowables();
228     }
229
230     public int indexOfThrowable(Class JavaDoc type) {
231         return delegate.indexOfThrowable( type, 0 );
232     }
233
234     public int indexOfThrowable(Class JavaDoc type, int fromIndex) {
235         return delegate.indexOfThrowable( type, fromIndex );
236     }
237
238     public void printStackTrace() {
239         delegate.printStackTrace();
240     }
241
242     public void printStackTrace(PrintStream JavaDoc out) {
243         delegate.printStackTrace( out );
244     }
245
246     public void printStackTrace(PrintWriter JavaDoc out) {
247         delegate.printStackTrace( out );
248     }
249
250     public final void printPartialStackTrace(PrintWriter JavaDoc out) {
251         super.printStackTrace( out );
252     }
253
254 }
255
Popular Tags