KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > utility > NestingException


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: Utility for OpenUSS
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.utility;
10
11 import java.io.PrintWriter JavaDoc;
12
13 /**
14  * Root exception for all exceptions available.
15  * This code was taken from the article at the Java World
16  * Java World Tip 91 by Terren Suydam (c)
17  * "Use Nested Exception in a Multitiered Environment"
18  *
19  * @author B. Lofi Dewanto
20  * @version 1.0
21  */

22 import java.io.StringWriter JavaDoc;
23
24
25 public class NestingException extends Exception JavaDoc {
26     // Nested exception
27
private Throwable JavaDoc nestedException;
28
29     // String representation of stack trace - not transient!
30
private String JavaDoc stackTraceString;
31
32     /**
33  * java.lang.Exception constructors.
34  */

35     public NestingException() {
36     }
37
38     /**
39  * java.lang.Exception constructors.
40  */

41     public NestingException(String JavaDoc msg) {
42         super(msg);
43     }
44
45     /**
46  * Additional c'tors - nest the exceptions, storing the stack trace.
47  */

48     public NestingException(Throwable JavaDoc nestedException) {
49         this.nestedException = nestedException;
50         stackTraceString = generateStackTraceString(nestedException);
51     }
52
53     /**
54  * Additional c'tors - nest the exceptions, storing the stack trace.
55  */

56     public NestingException(String JavaDoc msg, Throwable JavaDoc nestedException) {
57         this(msg);
58         this.nestedException = nestedException;
59         stackTraceString = generateStackTraceString(nestedException);
60     }
61
62     /**
63  * Convert a stack trace to a String so it can be serialized.
64  */

65     static public String JavaDoc generateStackTraceString(Throwable JavaDoc t) {
66         StringWriter JavaDoc s = new StringWriter JavaDoc();
67         t.printStackTrace(new PrintWriter JavaDoc(s));
68
69         return s.toString();
70     }
71
72     /**
73  * Methods.
74  */

75     public Throwable JavaDoc getNestedException() {
76         return nestedException;
77     }
78
79     /**
80  * Descend through linked-list of nesting exceptions, & output trace.
81  * Note that this displays the 'deepest' trace first.
82  */

83     public String JavaDoc getStackTraceString() {
84         // If there's no nested exception, there's no stackTrace
85
if (nestedException == null) {
86             return null;
87         }
88
89         StringBuffer JavaDoc traceBuffer = new StringBuffer JavaDoc();
90
91         if (nestedException instanceof NestingException) {
92             traceBuffer.append(
93                     ((NestingException) nestedException).getStackTraceString());
94             traceBuffer.append("-------- nested by:\n");
95         }
96
97         traceBuffer.append(stackTraceString);
98
99         return traceBuffer.toString();
100     }
101
102     /**
103  * Overrides Exception.getMessage().
104  */

105     public String JavaDoc getMessage() {
106         // superMsg will contain whatever String was passed into the
107
// constructor, and null otherwise.
108
String JavaDoc superMsg = super.getMessage();
109
110         // if there's no nested exception, do like we would always do
111
if (getNestedException() == null) {
112             return superMsg;
113         }
114
115         StringBuffer JavaDoc theMsg = new StringBuffer JavaDoc();
116
117         // get the nested exception's message
118
String JavaDoc nestedMsg = getNestedException().getMessage();
119
120         if (superMsg != null) {
121             theMsg.append(superMsg).append(": ").append(nestedMsg);
122         } else {
123             theMsg.append(nestedMsg);
124         }
125
126         return theMsg.toString();
127     }
128
129     /**
130  * Overrides Exception.toString().
131  */

132     public String JavaDoc toString() {
133         StringBuffer JavaDoc theMsg = new StringBuffer JavaDoc(super.toString());
134
135         if (getNestedException() != null) {
136             theMsg.append("; \n\t---> nested ").append(getNestedException());
137         }
138
139         return theMsg.toString();
140     }
141 }
Popular Tags