home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / Throwable.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  121 lines

  1. /*
  2.  * @(#)Throwable.java    1.24 95/12/06  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * An object signalling that an exceptional condition has occurred.
  24.  * All exceptions are a subclass of Exception. An exception contains
  25.  * a snapshot of the execution stack, this snapshot is used to print
  26.  * a stack backtrace. An exception also contains a message string.
  27.  * Here is an example of how to catch an exception:
  28.  * <pre>
  29.  *    try {
  30.  *        int a[] = new int[2];
  31.  *        a[4];
  32.  *    } catch (ArrayIndexOutOfBoundsException e) {
  33.  *        System.out.println("an exception occurred: " + e.getMessage());
  34.  *        e.printStackTrace();
  35.  *    }
  36.  * </pre>
  37.  * @version     1.24, 06 Dec 1995
  38.  */
  39. public class Throwable {
  40.     /**
  41.      * Native code saves some indication of the stack backtrace in this
  42.      * slot.
  43.      */
  44.     private Object backtrace;    
  45.     
  46.     /**
  47.      * Specific details about the Throwable.  For example,
  48.      * for FileNotFoundThrowables, this contains the name of
  49.      * the file that could not be found.
  50.      */
  51.     private String detailMessage;
  52.  
  53.     /**
  54.      * Constructs a new Throwable with no detail message. The stack
  55.      * trace is automatically filled in.
  56.      */
  57.     public Throwable() {
  58.     fillInStackTrace();
  59.     }
  60.  
  61.     /**
  62.      * Constructs a new Throwable with the specified detail message.
  63.      * The stack trace is automatically filled in.
  64.      * @param message    the detailed message
  65.      */
  66.     public Throwable(String message) {
  67.     fillInStackTrace();
  68.     detailMessage = message;
  69.     }
  70.  
  71.     /**
  72.      * Gets the detail message of the Throwable.  A detail message
  73.      * is a String that describes the Throwable that has taken place.
  74.      * @return the detail message of the throwable.
  75.      */
  76.     public String getMessage() {
  77.     return detailMessage;
  78.     }
  79.  
  80.     /**
  81.      * Returns a short description of the Throwable.
  82.      */
  83.     public String toString() {
  84.     String s = getClass().getName();
  85.     String message = getMessage();
  86.     return (message != null) ? (s + ": " + message) : s;
  87.     }
  88.  
  89.     /**
  90.      * Prints the Throwable and the Throwable's stack trace.
  91.      */
  92.     public void printStackTrace() { 
  93.         System.err.println(this);
  94.     printStackTrace0(System.err);
  95.     }
  96.  
  97.     public void printStackTrace(java.io.PrintStream s) { 
  98.         s.println(this);
  99.     printStackTrace0(s);
  100.     }
  101.  
  102.     private native void printStackTrace0(java.io.PrintStream s);
  103.  
  104.     /**
  105.      * Fills in the excecution stack trace. This is useful only
  106.      * when rethrowing a Throwable. For example:
  107.      * <p>
  108.      * <pre>
  109.      *       try {
  110.      *            a = b / c;
  111.      *       } catch(ArithmeticThrowable e) {
  112.      *        a = Number.MAX_VALUE;
  113.      *            throw e.fillInStackTrace();
  114.      *       }
  115.      * </pre>
  116.      * @return the Throwable itself.
  117.      * @see Throwable#printStackTrace
  118.      */
  119.     public native Throwable fillInStackTrace();
  120. }
  121.