public class Throwable
- Object
- Throwable
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, Error and 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:
Since: JDK1.0, CLDC 1.0
Constructors
public Throwable() | Constructs a new Throwable with null as its error message string. |
public Throwable(Throwable cause) | Constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). |
public Throwable(String message) | Constructs a new Throwable with the specified error message. |
public Throwable(String message, Throwable cause) | Constructs a new throwable with the specified detail message and cause. |
Methods
public Throwable initCause(Throwable cause) | Deprecated |
public Throwable getCause() | Returns the cause of this throwable or null if the cause is nonexistent or unknown. |
public String getMessage() | Returns the error message string of this Throwable object. |
public void printStackTrace() | Prints this Throwable and its backtrace to the standard error stream. |
public void printStackTrace(PrintStream s) | Prints this throwable and its backtrace to the given stream. |
public String toString() | Returns a short description of this Throwable object. |
public final void addSuppressed(Throwable exception) | |
public StackTraceElement[] getStackTrace() | |
public void setStackTrace(StackTraceElement[] el) | |
public final Throwable[] getSuppressed() | |
public String getLocalizedMessage() |
Inherited methods
Constructor details
Throwable
public Throwable()Constructs a new Throwable with null as its error message string.
Throwable
public Throwable(Throwable cause)Constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).
Parameters
causeThrowable- The cause
Throwable
public Throwable(String message)Constructs a new Throwable with the specified error message.
message - the error message. The error message is saved for later retrieval by the
method.
Throwable
public Throwable(String message, Throwable cause)Constructs a new throwable with the specified detail message and cause.
Parameters
messageString- The error message.
causeThrowable- The cause
Method details
initCause
public Throwable initCause(Throwable cause)Deprecated. DO NOT USE THIS METHOD, its here just to get the compiler working and isn’t intended for use
getCause
public Throwable getCause()Returns the cause of this throwable or null if the cause is nonexistent or unknown.
getMessage
public String getMessage()Returns the error message string of this Throwable object.
printStackTrace
public void printStackTrace()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
method for this object.
The format of the backtrace information depends on the implementation.
printStackTrace
public void printStackTrace(PrintStream s)Prints this throwable and its backtrace to the given stream. On the ParparVM ports this
writes the pre-rendered native stack (the C shadow-call-stack text, or the JavaScript
engine’s Error().stack on the JS port), which the crash reporter captures as the raw stack.
toString
public String toString()Returns a short description of this Throwable object. If this Throwable object was
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
method for this object If this Throwable object was
with no error message string, then the name of the actual class of this object is returned.
addSuppressed
public final void addSuppressed(Throwable exception)getStackTrace
public StackTraceElement[] getStackTrace()setStackTrace
public void setStackTrace(StackTraceElement[] el)getSuppressed
public final Throwable[] getSuppressed()getLocalizedMessage
public String getLocalizedMessage()