FileDocCategorySizeDatePackage
Throwable.javaAPI DocphoneME MR2 API (J2ME)6845Wed May 02 17:59:54 BST 2007java.lang

Throwable

public class Throwable extends Object
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or of one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Instances of two subclasses, {@link java.lang.Error} and {@link java.lang.Exception}, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).

By convention, class Throwable and its subclasses have two constructors, one that takes no arguments and one that takes a String argument that can be used to produce an error message.

A Throwable class contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error.

Here is one example of catching an exception:

try {
int a[] = new int[2];
a[4];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("exception: " + e.getMessage());
e.printStackTrace();
}
version
1.43, 12/04/99 (CLDC 1.0, Spring 2000)
since
JDK1.0

Fields Summary
private String
detailMessage
WARNING: this must be the first variable. Specific details about the Throwable. For example, for FileNotFoundThrowables, this contains the name of the file that could not be found.
private transient Object
backtrace
WARNING: this must be the second variable. Native code saves some indication of the stack backtrace in this slot.
Constructors Summary
public Throwable()
Constructs a new Throwable with null as its error message string.

        fillInStackTrace(); // Added for this VM
    
public Throwable(String message)
Constructs a new Throwable with the specified error message.

param
message the error message. The error message is saved for later retrieval by the {@link #getMessage()} method.

        fillInStackTrace(); // Added for this VM
        detailMessage = message;
    
Methods Summary
private native voidfillInStackTrace()
Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread. This method is useful when an application is re-throwing an error or exception. For example:

try {
a = b / c;
} catch(ArithmeticThrowable e) {
a = Double.MAX_VALUE;
throw e.fillInStackTrace();
}

return
this Throwable object.
see
java.lang.Throwable#printStackTrace()

public java.lang.StringgetMessage()
Returns the error message string of this throwable object.

return
the error message string of this Throwable object if it was {@link #Throwable(String) created} with an error message string; or null if it was {@link #Throwable() created} with no error message.

        return detailMessage;
    
public native voidprintStackTrace()
Prints this Throwable and its backtrace to the standard error stream. This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. The first line of output contains the result of the {@link #toString()} method for this object.

The format of the backtrace information depends on the implementation.

public java.lang.StringtoString()
Returns a short description of this throwable object. If this Throwable object was {@link #Throwable(String) created} with an error message string, then the result is the concatenation of three strings:
  • The name of the actual class of this object
  • ": " (a colon and a space)
  • The result of the {@link #getMessage} method for this object
If this Throwable object was {@link #Throwable() created} with no error message string, then the name of the actual class of this object is returned.

return
a string representation of this Throwable.

        String s = getClass().getName();
        String message = getMessage();
        return (message != null) ? (s + ": " + message) : s;