1 |
/* java.lang.Throwable |
/* java.lang.Throwable -- Reference implementation of root class for |
2 |
Copyright (C) 1998 Free Software Foundation, Inc. |
all Exceptions and Errors |
3 |
|
Copyright (C) 1998, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
6 |
|
|
8 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2, or (at your option) |
the Free Software Foundation; either version 2, or (at your option) |
10 |
any later version. |
any later version. |
11 |
|
|
12 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
46 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
47 |
import java.io.IOException; |
import java.io.IOException; |
48 |
|
|
49 |
|
/* |
50 |
|
* This class is a reference version, mainly for compiling a class library |
51 |
|
* jar. It is likely that VM implementers replace this with their own |
52 |
|
* version that can communicate effectively with the VM. |
53 |
|
*/ |
54 |
|
|
55 |
/** |
/** |
56 |
* Throwable is the superclass of all exceptions that can be raised. |
* Throwable is the superclass of all exceptions that can be raised. |
57 |
* |
* |
58 |
* @version 1.1.0, Oct 5 1998 |
* <p>There are two special cases: {@link Error} and {@link RuntimeException}: |
59 |
|
* these two classes (and their subclasses) are considered unchecked |
60 |
|
* exceptions, and are either frequent enough or catastrophic enough that you |
61 |
|
* do not need to declare them in <code>throws</code> clauses. Everything |
62 |
|
* else is a checked exception, and is ususally a subclass of |
63 |
|
* {@link Exception}; these exceptions have to be handled or declared. |
64 |
|
* |
65 |
|
* <p>Instances of this class are usually created with knowledge of the |
66 |
|
* execution context, so that you can get a stack trace of the problem spot |
67 |
|
* in the code. Also, since JDK 1.4, Throwables participate in "exception |
68 |
|
* chaining." This means that one exception can be caused by another, and |
69 |
|
* preserve the information of the original. |
70 |
|
* |
71 |
|
* <p>One reason this is useful is to wrap exceptions to conform to an |
72 |
|
* interface. For example, it would be bad design to require all levels |
73 |
|
* of a program interface to be aware of the low-level exceptions thrown |
74 |
|
* at one level of abstraction. Another example is wrapping a checked |
75 |
|
* exception in an unchecked one, to communicate that failure occured |
76 |
|
* while still obeying the method throws clause of a superclass. |
77 |
|
* |
78 |
|
* <p>A cause is assigned in one of two ways; but can only be assigned once |
79 |
|
* in the lifetime of the Throwable. There are new constructors added to |
80 |
|
* several classes in the exception hierarchy that directly initialize the |
81 |
|
* cause, or you can use the <code>initCause</code> method. This second |
82 |
|
* method is especially useful if the superclass has not been retrofitted |
83 |
|
* with new constructors:<br> |
84 |
|
* <pre> |
85 |
|
* try |
86 |
|
* { |
87 |
|
* lowLevelOp(); |
88 |
|
* } |
89 |
|
* catch (LowLevelException lle) |
90 |
|
* { |
91 |
|
* throw (HighLevelException) new HighLevelException().initCause(lle); |
92 |
|
* } |
93 |
|
* </pre> |
94 |
|
* Notice the cast in the above example; without it, your method would need |
95 |
|
* a throws clase that declared Throwable, defeating the purpose of chainig |
96 |
|
* your exceptions. |
97 |
|
* |
98 |
|
* <p>By convention, exception classes have two constructors: one with no |
99 |
|
* arguments, and one that takes a String for a detail message. Further, |
100 |
|
* classes which are likely to be used in an exception chain also provide |
101 |
|
* a constructor that takes a Throwable, with or without a detail message |
102 |
|
* string. |
103 |
|
* |
104 |
|
* <p>Another 1.4 feature is the StackTrace, a means of reflection that |
105 |
|
* allows the program to inspect the context of the exception, and which is |
106 |
|
* serialized, so that remote procedure calls can correctly pass exceptions. |
107 |
|
* |
108 |
* @author Brian Jones |
* @author Brian Jones |
109 |
* @author John Keiser |
* @author John Keiser |
110 |
* @since JDK1.0 |
* @author Eric Blake <ebb9@email.byu.edu> |
111 |
|
* @since 1.0 |
112 |
|
* @status still missing 1.4 functionality |
113 |
*/ |
*/ |
114 |
public class Throwable extends Object implements Serializable |
public class Throwable extends Object implements Serializable |
115 |
{ |
{ |
116 |
static final long serialVersionUID = -3042686055658047285L; |
/** |
117 |
|
* Compatible with JDK 1.0+. |
118 |
|
*/ |
119 |
|
private static final long serialVersionUID = -3042686055658047285L; |
120 |
|
|
121 |
|
/** |
122 |
|
* The detail message. |
123 |
|
* |
124 |
|
* @serial specific details about the exception, may be null |
125 |
|
* @XXX for serialization, renaming this detailMessage would be nice |
126 |
|
*/ |
127 |
private String message = null; |
private String message = null; |
128 |
|
|
129 |
/** |
/** |
130 |
* Instantiate this Throwable with an empty message. |
* The cause of the throwable, including null for an unknown or non-chained |
131 |
|
* cause. This may only be set once; so the field is set to |
132 |
|
* <code>this</code> until initialized. |
133 |
|
* |
134 |
|
* @serial the cause, or null if unknown, or this if not yet set |
135 |
|
* @since 1.4 |
136 |
|
* @XXX for 1.4 compatibility, add this field |
137 |
|
private Throwable cause = this; |
138 |
|
*/ |
139 |
|
|
140 |
|
/** |
141 |
|
* The stack trace, in a serialized form. |
142 |
|
* |
143 |
|
* @serial the elements of the stack trace; this is non-null, and has |
144 |
|
* no null entries |
145 |
|
* @since 1.4 |
146 |
|
* @XXX for 1.4 compatibility, add this field |
147 |
|
private StackTraceElement[] stackTrace; |
148 |
*/ |
*/ |
149 |
public Throwable() { |
|
150 |
|
/** |
151 |
|
* Instantiate this Throwable with an empty message. The cause remains |
152 |
|
* uninitialized. {@link #fillInStackTrace()} will be called to set |
153 |
|
* up the stack trace. |
154 |
|
*/ |
155 |
|
public Throwable() |
156 |
|
{ |
157 |
this(null); |
this(null); |
158 |
} |
} |
159 |
|
|
160 |
/** |
/** |
161 |
* Instantiate this Throwable with the given message. |
* Instantiate this Throwable with the given message. The cause remains |
162 |
* @param message the message to associate with the Throwable. |
* uninitialized. {@link #fillInStackTrace()} will be called to set |
163 |
|
* up the stack trace. |
164 |
|
* |
165 |
|
* @param message the message to associate with the Throwable |
166 |
*/ |
*/ |
167 |
public Throwable(String message) { |
public Throwable(String message) |
168 |
|
{ |
169 |
fillInStackTrace(); |
fillInStackTrace(); |
170 |
this.message = message; |
this.message = message; |
171 |
} |
} |
172 |
|
|
173 |
|
/** |
174 |
|
* Instantiate this Throwable with the given message and cause. Note that |
175 |
|
* the message is unrelated to the message of the cause. |
176 |
|
* {@link #fillInStackTrace()} will be called to set up the stack trace. |
177 |
|
* |
178 |
|
* @param message the message to associate with the Throwable |
179 |
|
* @param cause the cause, may be null |
180 |
|
* @since 1.4 |
181 |
|
* @XXX for 1.4 compatibility, add this constructor |
182 |
|
public Throwable(String message, Throwable cause) |
183 |
|
{ |
184 |
|
this(message); |
185 |
|
initCause(cause); |
186 |
|
} |
187 |
|
*/ |
188 |
|
|
189 |
|
/** |
190 |
|
* Instantiate this Throwable with the given cause. The message is then |
191 |
|
* built as <code>cause == null ? null : cause.toString()</code>. |
192 |
|
* {@link #fillInStackTrace()} will be called to set up the stack trace. |
193 |
|
* |
194 |
|
* @param cause the cause, may be null |
195 |
|
* @since 1.4 |
196 |
|
* @XXX for 1.4 compatibility, add this constructor |
197 |
|
public Throwable(Throwable cause) |
198 |
|
{ |
199 |
|
this(cause == null ? null : cause.toString(), cause); |
200 |
|
} |
201 |
|
*/ |
202 |
|
|
203 |
/** |
/** |
204 |
* Get the message associated with this Throwable. |
* Get the message associated with this Throwable. |
205 |
* @return the error message associated with this Throwable. |
* |
206 |
|
* @return the error message associated with this Throwable, may be null |
207 |
*/ |
*/ |
208 |
public String getMessage() { |
public String getMessage() |
209 |
|
{ |
210 |
return message; |
return message; |
211 |
} |
} |
212 |
|
|
213 |
/** |
/** |
214 |
* Get a localized version of this Throwable's error message. |
* Get a localized version of this Throwable's error message. |
215 |
* This method must be overridden in a subclass of Throwable |
* This method must be overridden in a subclass of Throwable |
216 |
* to actually produce locale-specific methods. The Throwable |
* to actually produce locale-specific methods. The Throwable |
217 |
* implementation just returns getMessage(). |
* implementation just returns getMessage(). |
218 |
* |
* |
219 |
* @return a localized version of this error message. |
* @return a localized version of this error message |
220 |
|
* @see #getMessage() |
221 |
|
* @since 1.1 |
222 |
*/ |
*/ |
223 |
public String getLocalizedMessage() { |
public String getLocalizedMessage() |
224 |
|
{ |
225 |
return getMessage(); |
return getMessage(); |
226 |
} |
} |
227 |
|
|
228 |
|
/** |
229 |
|
* Returns the cause of this exception, or null if the cause is not known |
230 |
|
* or non-existant. This cause is initialized by the new constructors, |
231 |
|
* or by calling initCause. |
232 |
|
* |
233 |
|
* @return the cause of this Throwable |
234 |
|
* @since 1.4 |
235 |
|
* @XXX for 1.4 compatibility, add this method |
236 |
|
public Throwable getCause() |
237 |
|
{ |
238 |
|
return cause == this ? null : cause; |
239 |
|
} |
240 |
|
*/ |
241 |
|
|
242 |
|
/** |
243 |
|
* Initialize the cause of this Throwable. This may only be called once |
244 |
|
* during the object lifetime, including implicitly by chaining |
245 |
|
* constructors. |
246 |
|
* |
247 |
|
* @param cause the cause of this Throwable, may be null |
248 |
|
* @return this |
249 |
|
* @throws IllegalArgumentException if cause is this (a Throwable can't be |
250 |
|
* its own cause!) |
251 |
|
* @throws IllegalStateException if the cause has already been set |
252 |
|
* @since 1.4 |
253 |
|
* @XXX for 1.4 compatibility, add this method |
254 |
|
public Throwable initCause(Throwable cause) |
255 |
|
{ |
256 |
|
if (cause == this) |
257 |
|
throw new IllegalArgumentException(); |
258 |
|
if (this.cause != this) |
259 |
|
throw new IllegalStateException(); |
260 |
|
this.cause = cause; |
261 |
|
return this; |
262 |
|
} |
263 |
|
*/ |
264 |
|
|
265 |
/** |
/** |
266 |
* Get a human-readable representation of this Throwable. |
* Get a human-readable representation of this Throwable. With a null |
267 |
* @return a human-readable String represting this Throwable. |
* detail message, this string is simply the object's class name. With |
268 |
* @XXX find out what exactly this should look like. |
* a detail message, the string is |
269 |
|
* <code>getClass().getName() + ": " + getMessage()</code>. |
270 |
|
* |
271 |
|
* @return a human-readable String represting this Throwable |
272 |
*/ |
*/ |
273 |
public String toString() { |
public String toString() |
274 |
|
{ |
275 |
return getClass().getName() + (message != null ? ": " + message : ""); |
return getClass().getName() + (message != null ? ": " + message : ""); |
276 |
} |
} |
277 |
|
|
278 |
/** |
/** |
279 |
* Print a stack trace to the standard error stream. |
* Print a stack trace to the standard error stream. This stream is the |
280 |
|
* current contents of <code>System.err</code>. The first line of output |
281 |
|
* is the result of {@link #toString()}, and the remaining lines represent |
282 |
|
* the data created by {@link #fillInStackTrace()}. While the format is |
283 |
|
* unspecified, this implementation uses the suggested format, demonstrated |
284 |
|
* by this example:<br> |
285 |
|
* <pre> |
286 |
|
* public class Junk |
287 |
|
* { |
288 |
|
* public static void main(String args[]) |
289 |
|
* { |
290 |
|
* try |
291 |
|
* { |
292 |
|
* a(); |
293 |
|
* } |
294 |
|
* catch(HighLevelException e) |
295 |
|
* { |
296 |
|
* e.printStackTrace(); |
297 |
|
* } |
298 |
|
* } |
299 |
|
* static void a() throws HighLevelException |
300 |
|
* { |
301 |
|
* try |
302 |
|
* { |
303 |
|
* b(); |
304 |
|
* } |
305 |
|
* catch(MidLevelException e) |
306 |
|
* { |
307 |
|
* throw new HighLevelException(e); |
308 |
|
* } |
309 |
|
* } |
310 |
|
* static void b() throws MidLevelException |
311 |
|
* { |
312 |
|
* c(); |
313 |
|
* } |
314 |
|
* static void c() throws MidLevelException |
315 |
|
* { |
316 |
|
* try |
317 |
|
* { |
318 |
|
* d(); |
319 |
|
* } |
320 |
|
* catch(LowLevelException e) |
321 |
|
* { |
322 |
|
* throw new MidLevelException(e); |
323 |
|
* } |
324 |
|
* } |
325 |
|
* static void d() throws LowLevelException |
326 |
|
* { |
327 |
|
* e(); |
328 |
|
* } |
329 |
|
* static void e() throws LowLevelException |
330 |
|
* { |
331 |
|
* throw new LowLevelException(); |
332 |
|
* } |
333 |
|
* } |
334 |
|
* class HighLevelException extends Exception |
335 |
|
* { |
336 |
|
* HighLevelException(Throwable cause) { super(cause); } |
337 |
|
* } |
338 |
|
* class MidLevelException extends Exception |
339 |
|
* { |
340 |
|
* MidLevelException(Throwable cause) { super(cause); } |
341 |
|
* } |
342 |
|
* class LowLevelException extends Exception |
343 |
|
* { |
344 |
|
* } |
345 |
|
* </pre> |
346 |
|
* <p> |
347 |
|
* <pre> |
348 |
|
* HighLevelException: MidLevelException: LowLevelException |
349 |
|
* at Junk.a(Junk.java:13) |
350 |
|
* at Junk.main(Junk.java:4) |
351 |
|
* Caused by: MidLevelException: LowLevelException |
352 |
|
* at Junk.c(Junk.java:23) |
353 |
|
* at Junk.b(Junk.java:17) |
354 |
|
* at Junk.a(Junk.java:11) |
355 |
|
* ... 1 more |
356 |
|
* Caused by: LowLevelException |
357 |
|
* at Junk.e(Junk.java:30) |
358 |
|
* at Junk.d(Junk.java:27) |
359 |
|
* at Junk.c(Junk.java:21) |
360 |
|
* ... 3 more |
361 |
|
* </pre> |
362 |
*/ |
*/ |
363 |
public void printStackTrace() { |
public void printStackTrace() |
364 |
|
{ |
365 |
printStackTrace(System.err); |
printStackTrace(System.err); |
366 |
} |
} |
367 |
|
|
368 |
/** |
/** |
369 |
* Print a stack trace to the specified PrintStream. |
* Print a stack trace to the specified PrintStream. See |
370 |
* @param s the PrintStream to write the trace to. |
* {@link #printStackTrace()} for the sample format. |
371 |
|
* |
372 |
|
* @param s the PrintStream to write the trace to |
373 |
*/ |
*/ |
374 |
public void printStackTrace(PrintStream s) { |
public void printStackTrace(PrintStream s) |
375 |
|
{ |
376 |
s.println(toString()); |
s.println(toString()); |
377 |
printStackTrace0 (s); |
printStackTrace0(s); |
378 |
} |
} |
379 |
|
|
380 |
/** |
/** |
381 |
* Print a stack trace to the specified PrintWriter. |
* Print a stack trace to the specified PrintWriter. See |
382 |
* @param w the PrintWriter to write the trace to. |
* {@link #printStackTrace()} for the sample format. |
383 |
|
* |
384 |
|
* @param w the PrintWriter to write the trace to |
385 |
|
* @since 1.1 |
386 |
*/ |
*/ |
387 |
public void printStackTrace(PrintWriter w) { |
public void printStackTrace(PrintWriter w) |
388 |
|
{ |
389 |
w.println(toString()); |
w.println(toString()); |
390 |
printStackTrace0 (w); |
printStackTrace0(w); |
391 |
} |
} |
392 |
|
|
393 |
|
/** |
394 |
|
* The implentation for printing a stack trace. |
395 |
|
* |
396 |
|
* @param stream either a PrintStream or PrintWriter |
397 |
|
*/ |
398 |
private native void printStackTrace0 (Object stream); |
private native void printStackTrace0 (Object stream); |
399 |
|
|
400 |
/** |
/** |
401 |
* Fill in the stack trace with the current execution stack. |
* Fill in the stack trace with the current execution stack. |
402 |
* Normally used when rethrowing an exception, to strip |
* Normally used when rethrowing an exception, to strip |
403 |
* off unnecessary extra stack frames. |
* off unnecessary extra stack frames. |
404 |
* @return this same throwable. |
* |
405 |
|
* @return this same throwable |
406 |
|
* @see #printStackTrace() |
407 |
*/ |
*/ |
408 |
public native Throwable fillInStackTrace(); |
public native Throwable fillInStackTrace(); |
409 |
|
|
410 |
/** |
/** |
411 |
* Serialize the object in a manner binary compatible with the JDK 1.2 |
* Provides access to the information printed in {@link #printStackTrace()}. |
412 |
|
* The array is non-null, with no null entries, although the virtual |
413 |
|
* machine is allowed to skip stack frames. If the array is not 0-length, |
414 |
|
* then slot 0 holds the information on the stack frame where the Throwable |
415 |
|
* was created (or at least where <code>fillInStackTrace()</code> was |
416 |
|
* called). |
417 |
|
* |
418 |
|
* @return an array of stack trace information, as available from the VM |
419 |
|
* @since 1.4 |
420 |
|
* @XXX for 1.4 compatibility, add this method |
421 |
|
public StackTraceElement[] getStackTrace() |
422 |
|
{ |
423 |
|
return stackTrace; |
424 |
|
} |
425 |
*/ |
*/ |
426 |
private void writeObject(java.io.ObjectOutputStream s) |
|
427 |
|
/** |
428 |
|
* Change the stack trace manually. This method is designed for remote |
429 |
|
* procedure calls, which intend to alter the stack trace before or after |
430 |
|
* serialization according to the context of the remote call. |
431 |
|
* |
432 |
|
* @param stackTrace the new trace to use |
433 |
|
* @throws NullPointerException if stackTrace is null or has null elements |
434 |
|
* @since 1.4 |
435 |
|
* @XXX for 1.4 compatibility, add this method |
436 |
|
public void setStackTrace(StackTraceElement[] stackTrace) |
437 |
|
{ |
438 |
|
for (int i = stackTrace.length; --i >= 0; ) |
439 |
|
if (stackTrace[i] == null) |
440 |
|
throw new NullPointerException(); |
441 |
|
this.stackTrace = stackTrace; |
442 |
|
} |
443 |
|
*/ |
444 |
|
|
445 |
|
/** |
446 |
|
* Serialize the object in a manner binary compatible with the JDK 1.2. |
447 |
|
* |
448 |
|
* @param s the stream to write to |
449 |
|
* @throws IOException if the write fails |
450 |
|
*/ |
451 |
|
private void writeObject(ObjectOutputStream s) |
452 |
throws IOException |
throws IOException |
453 |
{ |
{ |
454 |
ObjectOutputStream.PutField oFields; |
ObjectOutputStream.PutField oFields; |
455 |
oFields = s.putFields(); |
oFields = s.putFields(); |
456 |
oFields.put("detailMessage", message); |
oFields.put("detailMessage", message); |
457 |
s.writeFields(); |
s.writeFields(); |
458 |
} |
} |
459 |
|
|
460 |
/** |
/** |
461 |
* Deserialize the object in a manner binary compatible with the JDK 1.2 |
* Deserialize the object in a manner binary compatible with the JDK 1.2. |
462 |
*/ |
* |
463 |
private void readObject(java.io.ObjectInputStream s) |
* @param s the stream to read from |
464 |
|
* @throws IOException if the read fails |
465 |
|
* @throws ClassNotFoundException if deserialization fails |
466 |
|
*/ |
467 |
|
private void readObject(ObjectInputStream s) |
468 |
throws IOException, ClassNotFoundException |
throws IOException, ClassNotFoundException |
469 |
{ |
{ |
470 |
ObjectInputStream.GetField oFields; |
ObjectInputStream.GetField oFields; |
471 |
oFields = s.readFields(); |
oFields = s.readFields(); |
472 |
message = (String)oFields.get("detailMessage", (String)null); |
message = (String)oFields.get("detailMessage", (String)null); |
473 |
} |
} |
474 |
} |
} |