changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / SSJava / Throwable.java
1 /* java.lang.Throwable -- Root class for all Exceptions and Errors
2    Copyright (C) 1998, 1999, 2002, 2004, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 //package java.lang;
39
40 /*import gnu.classpath.SystemProperties;
41
42 import gnu.java.lang.CPStringBuilder;
43
44 import java.io.PrintStream;
45 import java.io.PrintWriter;
46 import java.io.Serializable;*/
47
48 /**
49  * Throwable is the superclass of all exceptions that can be raised.
50  *
51  * <p>There are two special cases: {@link Error} and {@link RuntimeException}:
52  * these two classes (and their subclasses) are considered unchecked
53  * exceptions, and are either frequent enough or catastrophic enough that you
54  * do not need to declare them in <code>throws</code> clauses.  Everything
55  * else is a checked exception, and is ususally a subclass of
56  * {@link Exception}; these exceptions have to be handled or declared.
57  *
58  * <p>Instances of this class are usually created with knowledge of the
59  * execution context, so that you can get a stack trace of the problem spot
60  * in the code.  Also, since JDK 1.4, Throwables participate in "exception
61  * chaining."  This means that one exception can be caused by another, and
62  * preserve the information of the original.
63  *
64  * <p>One reason this is useful is to wrap exceptions to conform to an
65  * interface.  For example, it would be bad design to require all levels
66  * of a program interface to be aware of the low-level exceptions thrown
67  * at one level of abstraction. Another example is wrapping a checked
68  * exception in an unchecked one, to communicate that failure occured
69  * while still obeying the method throws clause of a superclass.
70  *
71  * <p>A cause is assigned in one of two ways; but can only be assigned once
72  * in the lifetime of the Throwable.  There are new constructors added to
73  * several classes in the exception hierarchy that directly initialize the
74  * cause, or you can use the <code>initCause</code> method. This second
75  * method is especially useful if the superclass has not been retrofitted
76  * with new constructors:<br>
77  * <pre>
78  * try
79  *   {
80  *     lowLevelOp();
81  *   }
82  * catch (LowLevelException lle)
83  *   {
84  *     throw (HighLevelException) new HighLevelException().initCause(lle);
85  *   }
86  * </pre>
87  * Notice the cast in the above example; without it, your method would need
88  * a throws clase that declared Throwable, defeating the purpose of chainig
89  * your exceptions.
90  *
91  * <p>By convention, exception classes have two constructors: one with no
92  * arguments, and one that takes a String for a detail message.  Further,
93  * classes which are likely to be used in an exception chain also provide
94  * a constructor that takes a Throwable, with or without a detail message
95  * string.
96  *
97  * <p>Another 1.4 feature is the StackTrace, a means of reflection that
98  * allows the program to inspect the context of the exception, and which is
99  * serialized, so that remote procedure calls can correctly pass exceptions.
100  *
101  * @author Brian Jones
102  * @author John Keiser
103  * @author Mark Wielaard
104  * @author Tom Tromey
105  * @author Eric Blake (ebb9@email.byu.edu)
106  * @since 1.0
107  * @status updated to 1.4
108  */
109 public class Throwable //implements Serializable
110 {
111   /**
112    * Compatible with JDK 1.0+.
113    */
114   private static final long serialVersionUID = -3042686055658047285L;
115
116   /**
117    * The detail message.
118    *
119    * @serial specific details about the exception, may be null
120    */
121   private final String detailMessage;
122
123   /**
124    * The cause of the throwable, including null for an unknown or non-chained
125    * cause. This may only be set once; so the field is set to
126    * <code>this</code> until initialized.
127    *
128    * @serial the cause, or null if unknown, or this if not yet set
129    * @since 1.4
130    */
131   private Throwable cause = null;//this;
132
133   /**
134    * The stack trace, in a serialized form.
135    *
136    * @serial the elements of the stack trace; this is non-null, and has
137    *         no null entries
138    * @since 1.4
139    */
140   //private StackTraceElement[] stackTrace;
141
142   /**
143    * Instantiate this Throwable with an empty message. The cause remains
144    * uninitialized.  {@link #fillInStackTrace()} will be called to set
145    * up the stack trace.
146    */
147   public Throwable()
148   {
149     //this((String) null);
150     detailMessage = null;
151   }
152
153   /**
154    * Instantiate this Throwable with the given message. The cause remains
155    * uninitialized.  {@link #fillInStackTrace()} will be called to set
156    * up the stack trace.
157    *
158    * @param message the message to associate with the Throwable
159    */
160   public Throwable(String message)
161   {
162     //fillInStackTrace();
163     detailMessage = message;
164   }
165
166   /**
167    * Instantiate this Throwable with the given message and cause. Note that
168    * the message is unrelated to the message of the cause.
169    * {@link #fillInStackTrace()} will be called to set up the stack trace.
170    *
171    * @param message the message to associate with the Throwable
172    * @param cause the cause, may be null
173    * @since 1.4
174    */
175   public Throwable(String message, Throwable cause)
176   {
177     //this(message);
178     detailMessage = message;
179     this.cause = cause;
180   }
181
182   /**
183    * Instantiate this Throwable with the given cause. The message is then
184    * built as <code>cause == null ? null : cause.toString()</code>.
185    * {@link #fillInStackTrace()} will be called to set up the stack trace.
186    *
187    * @param cause the cause, may be null
188    * @since 1.4
189    */
190   public Throwable(Throwable causem)
191   {
192     //this(cause == null ? null : cause.toString(), cause);
193     String message = causem == null ? null : causem.toString();
194     detailMessage = message;
195     this.cause = causem;
196   }
197
198   /**
199    * Get the message associated with this Throwable.
200    *
201    * @return the error message associated with this Throwable, may be null
202    */
203   public String getMessage()
204   {
205     return detailMessage;
206   }
207
208   /**
209    * Get a localized version of this Throwable's error message.
210    * This method must be overridden in a subclass of Throwable
211    * to actually produce locale-specific methods.  The Throwable
212    * implementation just returns getMessage().
213    *
214    * @return a localized version of this error message
215    * @see #getMessage()
216    * @since 1.1
217    */
218   public String getLocalizedMessage()
219   {
220     return getMessage();
221   }
222
223   /**
224    * Returns the cause of this exception, or null if the cause is not known
225    * or non-existant. This cause is initialized by the new constructors,
226    * or by calling initCause.
227    *
228    * @return the cause of this Throwable
229    * @since 1.4
230    */
231   public Throwable getCause()
232   {
233     return cause == this ? null : cause;
234   }
235
236   /**
237    * Initialize the cause of this Throwable.  This may only be called once
238    * during the object lifetime, including implicitly by chaining
239    * constructors.
240    *
241    * @param cause the cause of this Throwable, may be null
242    * @return this
243    * @throws IllegalArgumentException if cause is this (a Throwable can't be
244    *         its own cause!)
245    * @throws IllegalStateException if the cause has already been set
246    * @since 1.4
247    */
248   public Throwable initCause(Throwable cause)
249   {
250     /*if (cause == this)
251       throw new IllegalArgumentException();
252     if (this.cause != this)
253       throw new IllegalStateException();*/
254     this.cause = cause;
255     return this;
256   }
257
258   /**
259    * Get a human-readable representation of this Throwable. The detail message
260    * is retrieved by getLocalizedMessage().  Then, with a null detail
261    * message, this string is simply the object's class name; otherwise
262    * the string is <code>getClass().getName() + ": " + message</code>.
263    *
264    * @return a human-readable String represting this Throwable
265    */
266   /*public String toString()
267   {
268     String msg = getLocalizedMessage();
269     return getClass().getName() + (msg == null ? "" : ": " + msg);
270   }*/
271
272   /**
273    * Print a stack trace to the standard error stream. This stream is the
274    * current contents of <code>System.err</code>. The first line of output
275    * is the result of {@link #toString()}, and the remaining lines represent
276    * the data created by {@link #fillInStackTrace()}. While the format is
277    * unspecified, this implementation uses the suggested format, demonstrated
278    * by this example:<br>
279    * <pre>
280    * public class Junk
281    * {
282    *   public static void main(String args[])
283    *   {
284    *     try
285    *       {
286    *         a();
287    *       }
288    *     catch(HighLevelException e)
289    *       {
290    *         e.printStackTrace();
291    *       }
292    *   }
293    *   static void a() throws HighLevelException
294    *   {
295    *     try
296    *       {
297    *         b();
298    *       }
299    *     catch(MidLevelException e)
300    *       {
301    *         throw new HighLevelException(e);
302    *       }
303    *   }
304    *   static void b() throws MidLevelException
305    *   {
306    *     c();
307    *   }
308    *   static void c() throws MidLevelException
309    *   {
310    *     try
311    *       {
312    *         d();
313    *       }
314    *     catch(LowLevelException e)
315    *       {
316    *         throw new MidLevelException(e);
317    *       }
318    *   }
319    *   static void d() throws LowLevelException
320    *   {
321    *     e();
322    *   }
323    *   static void e() throws LowLevelException
324    *   {
325    *     throw new LowLevelException();
326    *   }
327    * }
328    * class HighLevelException extends Exception
329    * {
330    *   HighLevelException(Throwable cause) { super(cause); }
331    * }
332    * class MidLevelException extends Exception
333    * {
334    *   MidLevelException(Throwable cause)  { super(cause); }
335    * }
336    * class LowLevelException extends Exception
337    * {
338    * }
339    * </pre>
340    * <p>
341    * <pre>
342    *  HighLevelException: MidLevelException: LowLevelException
343    *          at Junk.a(Junk.java:13)
344    *          at Junk.main(Junk.java:4)
345    *  Caused by: MidLevelException: LowLevelException
346    *          at Junk.c(Junk.java:23)
347    *          at Junk.b(Junk.java:17)
348    *          at Junk.a(Junk.java:11)
349    *          ... 1 more
350    *  Caused by: LowLevelException
351    *          at Junk.e(Junk.java:30)
352    *          at Junk.d(Junk.java:27)
353    *          at Junk.c(Junk.java:21)
354    *          ... 3 more
355    * </pre>
356    */
357   public void printStackTrace()
358   {
359     //printStackTrace(System.err);
360   }
361
362   /**
363    * Print a stack trace to the specified PrintStream. See
364    * {@link #printStackTrace()} for the sample format.
365    *
366    * @param s the PrintStream to write the trace to
367    */
368   /*public void printStackTrace(PrintStream s)
369   {
370     s.print(stackTraceString());
371   }*/
372
373   /**
374    * Prints the exception, the detailed message and the stack trace
375    * associated with this Throwable to the given <code>PrintWriter</code>.
376    * The actual output written is implemention specific. Use the result of
377    * <code>getStackTrace()</code> when more precise information is needed.
378    *
379    * <p>This implementation first prints a line with the result of this
380    * object's <code>toString()</code> method.
381    * <br>
382    * Then for all elements given by <code>getStackTrace</code> it prints
383    * a line containing three spaces, the string "at " and the result of calling
384    * the <code>toString()</code> method on the <code>StackTraceElement</code>
385    * object. If <code>getStackTrace()</code> returns an empty array it prints
386    * a line containing three spaces and the string
387    * "&lt;&lt;No stacktrace available&gt;&gt;".
388    * <br>
389    * Then if <code>getCause()</code> doesn't return null it adds a line
390    * starting with "Caused by: " and the result of calling
391    * <code>toString()</code> on the cause.
392    * <br>
393    * Then for every cause (of a cause, etc) the stacktrace is printed the
394    * same as for the top level <code>Throwable</code> except that as soon
395    * as all the remaining stack frames of the cause are the same as the
396    * the last stack frames of the throwable that the cause is wrapped in
397    * then a line starting with three spaces and the string "... X more" is
398    * printed, where X is the number of remaining stackframes.
399    *
400    * @param pw the PrintWriter to write the trace to
401    * @since 1.1
402    */
403   /*public void printStackTrace (PrintWriter pw)
404   {
405     pw.print(stackTraceString());
406   }*/
407
408   /*
409    * We use inner class to avoid a static initializer in this basic class.
410    */
411   /*private static class StaticData
412   {
413     static final String nl = SystemProperties.getProperty("line.separator");
414   }*/
415
416   // Create whole stack trace in a stringbuffer so we don't have to print
417   // it line by line. This prevents printing multiple stack traces from
418   // different threads to get mixed up when written to the same PrintWriter.
419   /*private String stackTraceString()
420   {
421     CPStringBuilder sb = new CPStringBuilder();
422
423     // Main stacktrace
424     StackTraceElement[] stack = getStackTrace();
425     stackTraceStringBuffer(sb, this.toString(), stack, 0);
426
427     // The cause(s)
428     Throwable cause = getCause();
429     while (cause != null)
430       {
431         // Cause start first line
432         sb.append("Caused by: ");
433
434         // Cause stacktrace
435         StackTraceElement[] parentStack = stack;
436         stack = cause.getStackTrace();
437         if (parentStack == null || parentStack.length == 0)
438           stackTraceStringBuffer(sb, cause.toString(), stack, 0);
439         else
440           {
441             int equal = 0; // Count how many of the last stack frames are equal
442             int frame = stack.length-1;
443             int parentFrame = parentStack.length-1;
444             while (frame > 0 && parentFrame > 0)
445               {
446                 if (stack[frame].equals(parentStack[parentFrame]))
447                   {
448                     equal++;
449                     frame--;
450                     parentFrame--;
451                   }
452                 else
453                   break;
454               }
455             stackTraceStringBuffer(sb, cause.toString(), stack, equal);
456           }
457         cause = cause.getCause();
458       }
459
460     return sb.toString();
461   }*/
462
463   // Adds to the given StringBuffer a line containing the name and
464   // all stacktrace elements minus the last equal ones.
465   /*private static void stackTraceStringBuffer(CPStringBuilder sb, String name,
466                                         StackTraceElement[] stack, int equal)
467   {
468     String nl = StaticData.nl;
469     // (finish) first line
470     sb.append(name);
471     sb.append(nl);
472
473     // The stacktrace
474     if (stack == null || stack.length == 0)
475       {
476         sb.append("   <<No stacktrace available>>");
477         sb.append(nl);
478       }
479     else
480       {
481         for (int i = 0; i < stack.length-equal; i++)
482           {
483             sb.append("   at ");
484             sb.append(stack[i] == null ? "<<Unknown>>" : stack[i].toString());
485             sb.append(nl);
486           }
487         if (equal > 0)
488           {
489             sb.append("   ...");
490             sb.append(equal);
491             sb.append(" more");
492             sb.append(nl);
493           }
494       }
495   }*/
496
497   /**
498    * Fill in the stack trace with the current execution stack.
499    *
500    * @return this same throwable
501    * @see #printStackTrace()
502    */
503   /*public Throwable fillInStackTrace()
504   {
505     vmState = VMThrowable.fillInStackTrace(this);
506     stackTrace = null; // Should be regenerated when used.
507
508     return this;
509   }*/
510
511   /**
512    * Provides access to the information printed in {@link #printStackTrace()}.
513    * The array is non-null, with no null entries, although the virtual
514    * machine is allowed to skip stack frames.  If the array is not 0-length,
515    * then slot 0 holds the information on the stack frame where the Throwable
516    * was created (or at least where <code>fillInStackTrace()</code> was
517    * called).
518    *
519    * @return an array of stack trace information, as available from the VM
520    * @since 1.4
521    */
522   /*public StackTraceElement[] getStackTrace()
523   {
524     if (stackTrace == null)
525       if (vmState == null)
526         stackTrace = new StackTraceElement[0];
527       else 
528         {
529           stackTrace = vmState.getStackTrace(this);
530           vmState = null; // No longer needed
531         }
532
533     return stackTrace;
534   }*/
535
536   /**
537    * Change the stack trace manually. This method is designed for remote
538    * procedure calls, which intend to alter the stack trace before or after
539    * serialization according to the context of the remote call.
540    * <p>
541    * The contents of the given stacktrace is copied so changes to the
542    * original array do not change the stack trace elements of this
543    * throwable.
544    *
545    * @param stackTrace the new trace to use
546    * @throws NullPointerException if stackTrace is null or has null elements
547    * @since 1.4
548    */
549   /*public void setStackTrace(StackTraceElement[] stackTrace)
550   {
551     int i = stackTrace.length;
552     StackTraceElement[] st = new StackTraceElement[i];
553
554     while (--i >= 0)
555       {
556         st[i] = stackTrace[i];
557         if (st[i] == null)
558           throw new NullPointerException("Element " + i + " null");
559       }
560
561     this.stackTrace = st;
562   }*/
563
564   /**
565    * VM state when fillInStackTrace was called.
566    * Used by getStackTrace() to get an array of StackTraceElements.
567    * Cleared when no longer needed.
568    */
569   //private transient VMThrowable vmState;
570 }