Class Throwable
- java.lang.Object
-
- java.lang.Throwable
-
- All Implemented Interfaces:
Serializable
public class Throwable extends Object implements Serializable
The superclass of all classes which can be thrown by the VM. The two direct subclasses are recoverable exceptions (Exception) and unrecoverable errors (Error). This class provides common methods for accessing a string message which provides extra information about the circumstances in which theThrowablewas created (basically an error message in most cases), and for saving a stack trace (that is, a record of the call stack at a particular point in time) which can be printed later.A
Throwablecan also include a cause, which is a nestedThrowablethat represents the original problem that led to thisThrowable. It is often used for wrapping various types of errors into a commonThrowablewithout losing the detailed original error information. When printing the stack trace, the trace of the cause is included.- See Also:
Error,Exception,RuntimeException, Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description Throwable()Constructs a newThrowablethat includes the current stack trace.Throwable(String detailMessage)Constructs a newThrowablewith the current stack trace and the given detail message.Throwable(String detailMessage, Throwable cause)Constructs a newThrowablewith the current stack trace, the given detail message and cause.protectedThrowable(String detailMessage, Throwable cause, boolean enableSuppression, boolean writableStackTrace)Constructs a newThrowablewith the current stack trace, the specified detail message and the specified cause.Throwable(Throwable cause)Constructs a newThrowablewith the current stack trace and the given cause.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSuppressed(Throwable throwable)Addsthrowableto the list of throwables suppressed by this.ThrowablefillInStackTrace()Records the stack trace from the point where this method has been called to thisThrowable.ThrowablegetCause()Returns the cause of thisThrowable, ornullif there is no cause.StringgetLocalizedMessage()Returns the detail message which was provided when thisThrowablewas created.StringgetMessage()Returns the detail message which was provided when thisThrowablewas created.StackTraceElement[]getStackTrace()Returns a clone of the array of stack trace elements of thisThrowable.Throwable[]getSuppressed()Returns the throwables suppressed by this.ThrowableinitCause(Throwable throwable)Initializes the cause of thisThrowable.voidprintStackTrace()Writes a printable representation of thisThrowable's stack trace to theSystem.errstream.voidprintStackTrace(PrintStream err)Writes a printable representation of thisThrowable's stack trace to the given print stream.voidprintStackTrace(PrintWriter err)Writes a printable representation of thisThrowable's stack trace to the specified print writer.voidsetStackTrace(StackTraceElement[] trace)Sets the array of stack trace elements.StringtoString()Returns a string containing a concise, human-readable description of this object.
-
-
-
Constructor Detail
-
Throwable
public Throwable()
Constructs a newThrowablethat includes the current stack trace.
-
Throwable
public Throwable(String detailMessage)
Constructs a newThrowablewith the current stack trace and the given detail message.
-
Throwable
public Throwable(String detailMessage, Throwable cause)
Constructs a newThrowablewith the current stack trace, the given detail message and cause.
-
Throwable
public Throwable(Throwable cause)
Constructs a newThrowablewith the current stack trace and the given cause.
-
Throwable
protected Throwable(String detailMessage, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
Constructs a newThrowablewith the current stack trace, the specified detail message and the specified cause.- Parameters:
enableSuppression- if false,addSuppressed(Throwable)will be a no-op.writableStackTrace- if false,fillInStackTrace()will not be called, this object'sstackTracewill be null, calls tofillInStackTrace()andsetStackTrace(java.lang.StackTraceElement[])will be no-ops, andgetStackTrace()will return a zero-length array.- Since:
- 1.7
-
-
Method Detail
-
fillInStackTrace
public Throwable fillInStackTrace()
Records the stack trace from the point where this method has been called to thisThrowable. This method is invoked by theThrowableconstructors.This method is public so that code (such as an RPC system) which catches a
Throwableand then re-throws it can replace the construction-time stack trace with a stack trace from the location where the exception was re-thrown, by callingfillInStackTrace.This method is non-final so that non-Java language implementations can disable VM stack traces for their language. Filling in the stack trace is relatively expensive. Overriding this method in the root of a language's exception hierarchy allows the language to avoid paying for something it doesn't need.
- Returns:
- this
Throwableinstance.
-
getMessage
public String getMessage()
Returns the detail message which was provided when thisThrowablewas created. Returnsnullif no message was provided at creation time.
-
getLocalizedMessage
public String getLocalizedMessage()
Returns the detail message which was provided when thisThrowablewas created. Returnsnullif no message was provided at creation time. Subclasses may override this method to return localized text for the message. Android returns the regular detail message.
-
getStackTrace
public StackTraceElement[] getStackTrace()
Returns a clone of the array of stack trace elements of thisThrowable. EachStackTraceElementrepresents an entry in the call stack. The element at position 0 is the top of the stack, that is, the stack frame where thisThrowableis thrown.- See Also:
printStackTrace()
-
setStackTrace
public void setStackTrace(StackTraceElement[] trace)
Sets the array of stack trace elements. EachStackTraceElementrepresents an entry in the call stack. A copy of the specified array is stored in thisThrowable. will be returned bygetStackTrace()and printed byprintStackTrace().- Parameters:
trace- the new array ofStackTraceElements. A copy of the array is stored in thisThrowable, so subsequent changes totracewill not change the call stack stored in thisThrowable.- Throws:
NullPointerException- if any element intraceisnull.- See Also:
printStackTrace()
-
printStackTrace
public void printStackTrace()
Writes a printable representation of thisThrowable's stack trace to theSystem.errstream.
-
printStackTrace
public void printStackTrace(PrintStream err)
Writes a printable representation of thisThrowable's stack trace to the given print stream. If theThrowablecontains acause, the method will be invoked recursively for the nestedThrowable.
-
printStackTrace
public void printStackTrace(PrintWriter err)
Writes a printable representation of thisThrowable's stack trace to the specified print writer. If theThrowablecontains acause, the method will be invoked recursively for the nestedThrowable.- Parameters:
err- the writer to write the stack trace on.
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
initCause
public Throwable initCause(Throwable throwable)
Initializes the cause of thisThrowable. The cause can only be initialized once.- Parameters:
throwable- the cause of thisThrowable.- Returns:
- this
Throwableinstance. - Throws:
IllegalArgumentException- ifThrowableis this object.IllegalStateException- if the cause has already been initialized.
-
getCause
public Throwable getCause()
Returns the cause of thisThrowable, ornullif there is no cause.
-
addSuppressed
public final void addSuppressed(Throwable throwable)
Addsthrowableto the list of throwables suppressed by this. The throwable will included when this exception's stack trace is printed.- Throws:
IllegalArgumentException- ifthrowable == this.NullPointerException- ifthrowable == null.- Since:
- 1.7
-
getSuppressed
public final Throwable[] getSuppressed()
Returns the throwables suppressed by this.- Since:
- 1.7
-
-