1. /*
  2. * @(#)Throwable.java 1.44 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. * The <code>Throwable</code> class is the superclass of all errors
  13. * and exceptions in the Java language. Only objects that are
  14. * instances of this class (or of one of its subclasses) are thrown
  15. * by the Java Virtual Machine or can be thrown by the Java
  16. * <code>throw</code> statement. Similarly, only this class or one of
  17. * its subclasses can be the argument type in a <code>catch</code>
  18. * clause.
  19. * <p>
  20. * Instances of two subclasses, {@link java.lang.Error} and
  21. * {@link java.lang.Exception}, are conventionally used to indicate
  22. * that exceptional situations have occurred. Typically, these instances
  23. * are freshly created in the context of the exceptional situation so
  24. * as to include relevant information (such as stack trace data).
  25. * <p>
  26. * By convention, class <code>Throwable</code> and its subclasses have
  27. * two constructors, one that takes no arguments and one that takes a
  28. * <code>String</code> argument that can be used to produce an error
  29. * message.
  30. * <p>
  31. * A <code>Throwable</code> class contains a snapshot of the
  32. * execution stack of its thread at the time it was created. It can
  33. * also contain a message string that gives more information about
  34. * the error.
  35. * <p>
  36. * Here is one example of catching an exception:
  37. * <p><blockquote><pre>
  38. * try {
  39. * int a[] = new int[2];
  40. * a[4];
  41. * } catch (ArrayIndexOutOfBoundsException e) {
  42. * System.out.println("exception: " + e.getMessage());
  43. * e.printStackTrace();
  44. * }
  45. * </pre></blockquote>
  46. *
  47. * @author unascribed
  48. * @version 1.44, 02/02/00
  49. * @since JDK1.0
  50. */
  51. public class Throwable implements java.io.Serializable {
  52. /**
  53. * Native code saves some indication of the stack backtrace in this
  54. * slot.
  55. */
  56. private transient Object backtrace;
  57. /**
  58. * Specific details about the Throwable. For example,
  59. * for FileNotFoundThrowables, this contains the name of
  60. * the file that could not be found.
  61. *
  62. * @serial
  63. */
  64. private String detailMessage;
  65. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  66. private static final long serialVersionUID = -3042686055658047285L;
  67. /**
  68. * Constructs a new <code>Throwable</code> with <code>null</code> as
  69. * its error message string. Also, the method
  70. * {@link #fillInStackTrace()} is called for this object.
  71. */
  72. public Throwable() {
  73. fillInStackTrace();
  74. }
  75. /**
  76. * Constructs a new <code>Throwable</code> with the specified error
  77. * message. Also, the method {@link #fillInStackTrace()} is called for
  78. * this object.
  79. *
  80. * @param message the error message. The error message is saved for
  81. * later retrieval by the {@link #getMessage()} method.
  82. */
  83. public Throwable(String message) {
  84. fillInStackTrace();
  85. detailMessage = message;
  86. }
  87. /**
  88. * Returns the error message string of this throwable object.
  89. *
  90. * @return the error message string of this <code>Throwable</code>
  91. * object if it was {@link #Throwable(String) created} with an
  92. * error message string; or <code>null</code> if it was
  93. * {@link #Throwable() created} with no error message.
  94. *
  95. */
  96. public String getMessage() {
  97. return detailMessage;
  98. }
  99. /**
  100. * Creates a localized description of this <code>Throwable</code>.
  101. * Subclasses may override this method in order to produce a
  102. * locale-specific message. For subclasses that do not override this
  103. * method, the default implementation returns the same result as
  104. * <code>getMessage()</code>.
  105. *
  106. * @return The localized description of this <code>Throwable</code>.
  107. * @since JDK1.1
  108. */
  109. public String getLocalizedMessage() {
  110. return getMessage();
  111. }
  112. /**
  113. * Returns a short description of this throwable object.
  114. * If this <code>Throwable</code> object was
  115. * {@link #Throwable(String) created} with an error message string,
  116. * then the result is the concatenation of three strings:
  117. * <ul>
  118. * <li>The name of the actual class of this object
  119. * <li>": " (a colon and a space)
  120. * <li>The result of the {@link #getMessage} method for this object
  121. * </ul>
  122. * If this <code>Throwable</code> object was {@link #Throwable() created}
  123. * with no error message string, then the name of the actual class of
  124. * this object is returned.
  125. *
  126. * @return a string representation of this <code>Throwable</code>.
  127. */
  128. public String toString() {
  129. String s = getClass().getName();
  130. String message = getLocalizedMessage();
  131. return (message != null) ? (s + ": " + message) : s;
  132. }
  133. /**
  134. * Prints this <code>Throwable</code> and its backtrace to the
  135. * standard error stream. This method prints a stack trace for this
  136. * <code>Throwable</code> object on the error output stream that is
  137. * the value of the field <code>System.err</code>. The first line of
  138. * output contains the result of the {@link #toString()} method for
  139. * this object. Remaining lines represent data previously recorded by
  140. * the method {@link #fillInStackTrace()}. The format of this
  141. * information depends on the implementation, but the following
  142. * example may be regarded as typical:
  143. * <blockquote><pre>
  144. * java.lang.NullPointerException
  145. * at MyClass.mash(MyClass.java:9)
  146. * at MyClass.crunch(MyClass.java:6)
  147. * at MyClass.main(MyClass.java:3)
  148. * </pre></blockquote>
  149. * This example was produced by running the program:
  150. * <blockquote><pre>
  151. *
  152. * class MyClass {
  153. *
  154. * public static void main(String[] argv) {
  155. * crunch(null);
  156. * }
  157. * static void crunch(int[] a) {
  158. * mash(a);
  159. * }
  160. *
  161. * static void mash(int[] b) {
  162. * System.out.println(b[0]);
  163. * }
  164. * }
  165. * </pre></blockquote>
  166. *
  167. * @see java.lang.System#err
  168. */
  169. public void printStackTrace() {
  170. synchronized (System.err) {
  171. System.err.println(this);
  172. printStackTrace0(System.err);
  173. }
  174. }
  175. /**
  176. * Prints this <code>Throwable</code> and its backtrace to the
  177. * specified print stream.
  178. *
  179. * @param s <code>PrintStream</code> to use for output
  180. */
  181. public void printStackTrace(java.io.PrintStream s) {
  182. synchronized (s) {
  183. s.println(this);
  184. printStackTrace0(s);
  185. }
  186. }
  187. /**
  188. * Prints this <code>Throwable</code> and its backtrace to the specified
  189. * print writer.
  190. *
  191. * @param s <code>PrintWriter</code> to use for output
  192. * @since JDK1.1
  193. */
  194. public void printStackTrace(java.io.PrintWriter s) {
  195. synchronized (s) {
  196. s.println(this);
  197. printStackTrace0(s);
  198. }
  199. }
  200. /* The given object must have a void println(char[]) method */
  201. private native void printStackTrace0(Object s);
  202. /**
  203. * Fills in the execution stack trace. This method records within this
  204. * <code>Throwable</code> object information about the current state of
  205. * the stack frames for the current thread. This method is useful when
  206. * an application is re-throwing an error or exception. For example:
  207. * <p><blockquote><pre>
  208. * try {
  209. * a = b / c;
  210. * } catch(ArithmeticThrowable e) {
  211. * a = Double.MAX_VALUE;
  212. * throw e.fillInStackTrace();
  213. * }
  214. * </pre></blockquote>
  215. *
  216. * @return this <code>Throwable</code> object.
  217. * @see java.lang.Throwable#printStackTrace()
  218. */
  219. public native Throwable fillInStackTrace();
  220. }