1 package org.apache.torque;
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
56
57 import java.io.PrintStream;
58 import java.io.PrintWriter;
59 import java.io.StringWriter;
60 import java.util.LinkedList;
61 import java.util.StringTokenizer;
62
63 /***
64 * This is a base class of runtime exeptions thrown by Torque. <p>
65 *
66 * This class represents a non-checked type exception (see
67 * {@link java.lang.RuntimeException}).
68 * It is intended to ease the debugging by carrying on the information about the
69 * exception which was caught and provoked throwing the current exception.
70 * Catching and rethrowing may occur multiple times, and provided that all
71 * exceptions except the first one are descendands of
72 * <code>TorqueRuntimeException</code>, when the exception is finally printed
73 * out using any of the <code>printStackTrace()</code> methods, the stacktrace
74 * will contain the information about all exceptions thrown and caught on the
75 * way.
76 *
77 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
78 * @version $Id: TorqueRuntimeException.java,v 1.7 2002/09/13 06:09:09 mpoeschl Exp $
79 */
80 public class TorqueRuntimeException
81 extends RuntimeException
82 {
83 /***
84 * Holds the reference to the exception or error that caused
85 * this exception to be thrown.
86 */
87 private Throwable nested = null;
88
89 /***
90 * Constructs a new <code>TorqueRuntimeException</code> without specified
91 * detail message.
92 */
93 public TorqueRuntimeException()
94 {
95 super();
96 }
97
98 /***
99 * Constructs a new <code>TorqueRuntimeException</code> with specified
100 * detail message.
101 *
102 * @param msg the error message.
103 */
104 public TorqueRuntimeException(String msg)
105 {
106 super(msg);
107 }
108
109 /***
110 * Constructs a new <code>TorqueRuntimeException</code> with specified
111 * nested <code>Throwable</code>.
112 *
113 * @param nested the exception or error that caused this exception
114 * to be thrown.
115 */
116 public TorqueRuntimeException(Throwable nested)
117 {
118 super();
119 this.nested = nested;
120 }
121
122 /***
123 * Constructs a new <code>TorqueRuntimeException</code> with specified
124 * detail message and nested <code>Throwable</code>.
125 *
126 * @param msg the error message.
127 * @param nested the exception or error that caused this exception
128 * to be thrown.
129 */
130 public TorqueRuntimeException(String msg, Throwable nested)
131 {
132 super(msg);
133 this.nested = nested;
134 }
135
136 /***
137 * Prints the stack trace of this exception the the standar error stream.
138 */
139 public void printStackTrace()
140 {
141 synchronized (System.err)
142 {
143 printStackTrace(System.err);
144 }
145 }
146
147 /***
148 * Prints the stack trace of this exception to the specified print stream.
149 *
150 * @param out <code>PrintStream</code> to use for output
151 */
152 public void printStackTrace(PrintStream out)
153 {
154 synchronized (out)
155 {
156 PrintWriter pw = new PrintWriter(out, false);
157 printStackTrace(pw);
158
159 pw.flush();
160 }
161 }
162
163 /***
164 * Prints the stack trace of this exception to the specified print writer.
165 *
166 * @param out <code>PrintWriter</code> to use for output.
167 */
168 public void printStackTrace(PrintWriter out)
169 {
170 synchronized (out)
171 {
172 printStackTrace(out, 0);
173 }
174 }
175
176 /***
177 * Prints the stack trace of this exception skiping a specified number
178 * of stack frames.
179 *
180 * @param out <code>PrintWriter</code> to use for output.
181 * @param skip the numbere of stack frames to skip.
182 */
183 public void printStackTrace(PrintWriter out, int skip)
184 {
185 String[] st = captureStackTrace();
186 if (nested != null)
187 {
188 if (nested instanceof TorqueRuntimeException)
189 {
190 ((TorqueRuntimeException) nested)
191 .printStackTrace(out, st.length - 2);
192 }
193 else if (nested instanceof TorqueException)
194 {
195 ((TorqueException) nested).printStackTrace(out);
196 }
197 else
198 {
199 String[] nst = captureStackTrace(nested);
200 for (int i = 0; i < nst.length - st.length + 2; i++)
201 {
202 out.println(nst[i]);
203 }
204 }
205 out.print("rethrown as ");
206 }
207 for (int i = 0; i < st.length - skip; i++)
208 {
209 out.println(st[i]);
210 }
211 }
212
213 /***
214 * Captures the stack trace associated with this exception.
215 *
216 * @return an array of Strings describing stack frames.
217 */
218 private String[] captureStackTrace()
219 {
220 StringWriter sw = new StringWriter();
221 super.printStackTrace(new PrintWriter(sw, true));
222 return splitStackTrace(sw.getBuffer().toString());
223 }
224
225 /***
226 * Captures the stack trace associated with a <code>Throwable</code>
227 * object.
228 *
229 * @param t the <code>Throwable</code>.
230 * @return an array of Strings describing stack frames.
231 */
232 private String[] captureStackTrace(Throwable t)
233 {
234 StringWriter sw = new StringWriter();
235 t.printStackTrace(new PrintWriter(sw, true));
236 return splitStackTrace(sw.getBuffer().toString());
237 }
238
239 /***
240 * Splits the stack trace given as a newline separated string
241 * into an array of stack frames.
242 *
243 * @param stackTrace the stack trace.
244 * @return an array of Strings describing stack frames.
245 */
246 private String[] splitStackTrace(String stackTrace)
247 {
248 String linebreak = System.getProperty("line.separator");
249 StringTokenizer st = new StringTokenizer(stackTrace, linebreak);
250 LinkedList list = new LinkedList();
251 while (st.hasMoreTokens())
252 {
253 list.add(st.nextToken());
254 }
255 return (String[]) list.toArray(new String[] {});
256 }
257 }