Package java.lang |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.lang.Throwable extends java.lang.Object { // Constructors public Throwable(); public Throwable(String message); // Methods public Throwable fillInStackTrace(); public String getMessage(); public void printStackTrace(); public void printStackTrace(PrintStream s); public String toString(); }
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.
A Throwable 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(); }
public Throwable()Constructs a new Throwable with no detail message. The stack trace is automatically filled in.
public Throwable(String message)Constructs a new Throwable with the specified detail message. The stack trace is automatically filled in.
Parameter Description message the detail message
public Throwable fillInStackTrace()Fills in the execution stack trace. This method is useful when an application is re-throwing an error or exception. For example:
try { a = b / c; } catch(ArithmeticThrowable e) { a = Number.MAX_VALUE; throw e.fillInStackTrace(); }Return Value:
Returns this Throwable object.
See Also: printStackTrace .
public String getMessage()Return Value:
Returns the detail message of this Throwable, or null if this Throwable does not have a detailed message.
public void printStackTrace()Prints this Throwable and its backtrace to the standard error output stream.
public void printStackTrace(PrintStream s)Prints this Throwable and its backtrace to the specified print stream.
public String toString()Return Value:
Returns a string representation of this Throwable.
Overrides:
toString in class Object .