Class Throwable
- All Implemented Interfaces:
Serializable
public class Throwable extends Object implements Serializable
Exception) and
unrecoverable errors (Error). This class provides common methods for
accessing a string message which provides extra information about the
circumstances in which the Throwable was 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 Throwable can also include a cause, which is a nested
Throwable that represents the original problem that led to this
Throwable. It is often used for wrapping various types of errors into a
common Throwable without 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
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 Details
-
Throwable
public Throwable()Constructs a newThrowablethat includes the current stack trace. -
Throwable
Constructs a newThrowablewith the current stack trace and the given detail message. -
Throwable
Constructs a newThrowablewith the current stack trace, the given detail message and cause. -
Throwable
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 Details
-
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
Returns the detail message which was provided when thisThrowablewas created. Returnsnullif no message was provided at creation time. -
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
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
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
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
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
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
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
Returns the cause of thisThrowable, ornullif there is no cause. -
addSuppressed
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
Returns the throwables suppressed by this.- Since:
- 1.7
-