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