KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > exception > ChainedException


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.kernel.exception;
66
67 import java.io.PrintStream JavaDoc;
68 import java.io.PrintWriter JavaDoc;
69
70
71 /**
72  * Enhanced Exception class to allow Exception chaining. This helps
73  * indicate the origin of an exception more clearly than simply passing
74  * on the original message
75  * Creation date: (4/15/00 5:47:38 PM)
76  *
77  * @author Michael Nash
78  */

79 public class ChainedException
80         extends Exception JavaDoc {
81     private Throwable JavaDoc nested = null;
82     private int errorNumber = 0;
83
84     /**
85      * Normal no-args constructor
86      */

87     public ChainedException() {
88         super();
89     } /* ChaninedException() */
90
91     /**
92      * Normal constructor with a single message
93      *
94      * @param s The exception message
95      */

96     public ChainedException(String JavaDoc s) {
97         super(s);
98     } /* Chained Exception */
99
100     /**
101      * Specify an error number
102      *
103      * @param s the message
104      * @param newErrorNumber The error number to assign
105      */

106     public ChainedException(String JavaDoc s, int newErrorNumber) {
107         super(s);
108         errorNumber = newErrorNumber;
109     }
110
111     /**
112      * Constructor with a single message and a nested exception
113      *
114      * @param message The exception message
115      * @param newNested The nested item
116      */

117     public ChainedException(String JavaDoc message, Throwable JavaDoc newNested) {
118         super(message);
119         nested = newNested;
120     } /* ChainedException(String, Throwable) */
121
122     /**
123      * Constructor with a single message and a nested exception with error number
124      *
125      * @param message The exception message
126      * @param newNested The nested item
127      * @param newErrorNumber the error number associated with the exception
128      */

129     public ChainedException(String JavaDoc message, Throwable JavaDoc newNested,
130                             int newErrorNumber) {
131         super(message);
132         nested = newNested;
133         errorNumber = newErrorNumber;
134     } /* ChainedException(String, Throwable) */
135
136     /**
137      * Constructor with no message and a nested exception
138      *
139      * @param newNested The nested exception
140      */

141     public ChainedException(Throwable JavaDoc newNested) {
142         nested = newNested;
143     } /* ChainedException(Throwable) */
144
145     /**
146      * Constructor with no message and a nested exception, but with an error number
147      *
148      * @param newNested The nested exception
149      * @param newErrorNumber the error number associated with the exception message
150      */

151     public ChainedException(Throwable JavaDoc newNested, int newErrorNumber) {
152         nested = newNested;
153         errorNumber = newErrorNumber;
154     } /* ChainedException(Throwable) */
155
156     /**
157      * Extend getMessage to return the nested message (if any) if we don't have one
158      *
159      * @return java.lang.String
160      */

161     public String JavaDoc getMessage() {
162         String JavaDoc myMessage = super.getMessage();
163
164         if ((myMessage == null) && (nested != null)) {
165             myMessage = ("Nested exception: " + omitPackages(nested) + ": " +
166                     nested.getMessage());
167         }
168
169         return myMessage;
170     } /* getMessage() */
171
172     /**
173      * utility to get just name of class
174      *
175      * @param obj the object of the given class, the name of which will be returned
176      * @return name of class, less any package prefix
177      */

178     public static String JavaDoc omitPackages(Object JavaDoc obj) {
179         int i = obj.getClass().getPackage().getName().length();
180         if (i > 0) {
181             i++;
182         }
183         return obj.getClass().getName().substring(i);
184     }
185
186     /**
187      * Return the error number if one was supplied
188      *
189      * @return integer: the error number specified (or zero if non was specified)
190      */

191     public int getErrorNumber() {
192         return errorNumber;
193     }
194
195     /**
196      * Extend printStackTrace to handle the nested exception correctly.
197      */

198     public void printStackTrace() {
199         if (nested != null) {
200             nested.printStackTrace();
201         }
202         super.printStackTrace();
203     } /* printStackTrace() */
204
205     /**
206      * Extend printStackTrace to handle the nested Exception
207      *
208      * @param p The PrintStream to write the exception messages into
209      */

210     public void printStackTrace(PrintStream JavaDoc p) {
211         if (nested != null) {
212             nested.printStackTrace(p);
213         }
214         super.printStackTrace(p);
215
216     } /* printStackTrace(PrintStream) */
217
218     /**
219      * Extend printStackTrace to handle the nested Exception
220      *
221      * @param p The PrintWriter to write the exception messages into
222      */

223     public void printStackTrace(PrintWriter JavaDoc p) {
224         if (nested != null) {
225             nested.printStackTrace(p);
226         }
227         super.printStackTrace(p);
228     } /* printStackTrace(PrintWriter) */
229
230     /**
231      * Retrieve the nested exception
232      *
233      * @return the nested Exception
234      */

235     public Throwable JavaDoc getNested() {
236         return nested;
237     } /* getNested() */
238
239 } /* ChainedException */
240
Popular Tags