home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Throwable.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.0 KB  |  175 lines

  1. /*
  2.  * @(#)Throwable.java    1.36 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The <code>Throwable</code> class is the superclass of all errors 
  19.  * and exceptions in the Java language. Only objects that are 
  20.  * instances of this class (or of one of its subclasses) are thrown 
  21.  * by the Java Virtual Machine or can be thrown by the Java 
  22.  * <code>throw</code> statement. Similarly, only this class or one of 
  23.  * its subclasses can be the argument type in a <code>catch</code> 
  24.  * clause. 
  25.  * <p>
  26.  * A <code>Throwable</code> class contains a snapshot of the 
  27.  * execution stack of its thread at the time it was created. It can 
  28.  * also contain a message string that gives more information about 
  29.  * the error. 
  30.  * <p>
  31.  * Here is one example of catching an exception: 
  32.  * <p><blockquote><pre>
  33.  *     try {
  34.  *         int a[] = new int[2];
  35.  *         a[4];
  36.  *     } catch (ArrayIndexOutOfBoundsException e) {
  37.  *         System.out.println("exception: " + e.getMessage());
  38.  *         e.printStackTrace();
  39.  *     }
  40.  * </pre></blockquote>
  41.  *
  42.  * @author  unascribed
  43.  * @version 1.31, 01/26/97
  44.  * @since   JDK1.0
  45.  */
  46. public class Throwable implements java.io.Serializable {
  47.     /**
  48.      * Native code saves some indication of the stack backtrace in this
  49.      * slot.
  50.      */
  51.     private transient Object backtrace;    
  52.  
  53.     /**
  54.      * Specific details about the Throwable.  For example,
  55.      * for FileNotFoundThrowables, this contains the name of
  56.      * the file that could not be found.
  57.      */
  58.     private String detailMessage;
  59.  
  60.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  61.     private static final long serialVersionUID = -3042686055658047285L;
  62.  
  63.     /**
  64.      * Constructs a new <code>Throwable</code> with no detail message. 
  65.      * The stack trace is automatically filled in. 
  66.      */
  67.     public Throwable() {
  68.     fillInStackTrace();
  69.     }
  70.  
  71.     /**
  72.      * Constructs a new <code>Throwable</code> with the specified detail 
  73.      * message. The stack trace is automatically filled in. 
  74.      *
  75.      * @param   message   the detail message.
  76.      */
  77.     public Throwable(String message) {
  78.     fillInStackTrace();
  79.     detailMessage = message;
  80.     }
  81.  
  82.     /**
  83.      * Returns the detail message of this throwable object.
  84.      *
  85.      * @return  the detail message of this <code>Throwable</code>,
  86.      *          or <code>null</code> if this <code>Throwable</code> does not
  87.      *          have a detail message.
  88.      */
  89.     public String getMessage() {
  90.     return detailMessage;
  91.     }
  92.  
  93.     /**
  94.      * Creates a localized description of this <code>Throwable</code>.
  95.      * Subclasses may override this method in order to produce a
  96.      * locale-specific message.  For subclasses that do not override this
  97.      * method, the default implementation returns the same result as
  98.      * <code>getMessage()</code>.
  99.      *
  100.      * @since   JDK1.1
  101.      */
  102.     public String getLocalizedMessage() {
  103.     return getMessage();
  104.     }
  105.  
  106.     /**
  107.      * Returns a short description of this throwable object.
  108.      *
  109.      * @return  a string representation of this <code>Throwable</code>.
  110.      */
  111.     public String toString() {
  112.     String s = getClass().getName();
  113.     String message = getLocalizedMessage();
  114.     return (message != null) ? (s + ": " + message) : s;
  115.     }
  116.  
  117.     /**
  118.      * Prints this <code>Throwable</code> and its backtrace to the 
  119.      * standard error stream. 
  120.      *
  121.      * @see     java.lang.System#err
  122.      */
  123.     public void printStackTrace() { 
  124.     synchronized (System.err) {
  125.         System.err.println(this);
  126.         printStackTrace0(System.err);
  127.     }
  128.     }
  129.  
  130.     /**
  131.      * Prints this <code>Throwable</code> and its backtrace to the 
  132.      * specified print stream. 
  133.      */
  134.     public void printStackTrace(java.io.PrintStream s) { 
  135.     synchronized (s) {
  136.         s.println(this);
  137.         printStackTrace0(s);
  138.     }
  139.     }
  140.  
  141.     /**
  142.      * Prints this <code>Throwable</code> and its backtrace to the specified
  143.      * print writer.
  144.      *
  145.      * @since   JDK1.1
  146.      */
  147.     public void printStackTrace(java.io.PrintWriter s) { 
  148.     synchronized (s) {
  149.         s.println(this);
  150.         printStackTrace0(s);
  151.     }
  152.     }
  153.  
  154.     /* The given object must have a void println(char[]) method */
  155.     private native void printStackTrace0(Object s);
  156.  
  157.     /**
  158.      * Fills in the execution stack trace. This method is useful when an 
  159.      * application is re-throwing an error or exception. For example: 
  160.      * <p><blockquote><pre>
  161.      *     try {
  162.      *         a = b / c;
  163.      *     } catch(ArithmeticThrowable e) {
  164.      *         a = Number.MAX_VALUE;
  165.      *         throw e.fillInStackTrace();
  166.      *     }
  167.      * </pre></blockquote>
  168.      *
  169.      * @return  this <code>Throwable</code> object.
  170.      * @see     java.lang.Throwable#printStackTrace()
  171.      */
  172.     public native Throwable fillInStackTrace();
  173.  
  174. }
  175.