home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Throwable.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  186 lines

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