java.lang.Object
java.lang.Throwable
- All Implemented Interfaces:
Serializable
This class is the superclass of all classes which
can be thrown by the virtual machine. The two direct
subclasses represent 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 the throwable was created, and for filling in a
walkback (i.e. a record of the call stack at a
particular point in time) which can be printed later.
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a new instance of this class with its walkback filled in.Constructs a new instance of this class with its walkback and message filled in.Constructs a new instance of this class with its walkback, message and cause filled in.protected
Throwable
(String detailMessage, Throwable throwable, boolean enableSuppression, boolean enableWritableStackTrace) Constructs a new instance of this class with its walkback, message and cause filled in.Constructs a new instance of this class with its walkback and cause filled in. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addSuppressed
(Throwable exception) The specified exception is going to be suppressed in order to give priority to this exception (primary exception) and to be appended to the list of suppressed exceptions.Record in the receiver a walkback from the point where this message was sent.getCause()
Answers the cause of this Throwable, or null if there is no cause.Answers the extra information message which was provided when the throwable was created.Answers the extra information message which was provided when the throwable was created.Answers an array of StackTraceElement.final Throwable[]
Returns an array of exceptions suppressed, typically by the automatically generated code from the try-with-resources statement, in order to give priority to this exception (primary exception).Initialize the cause of the receiver.void
Outputs a printable representation of the receiver's walkback on the System.err stream.void
Outputs a printable representation of the receiver's walkback on the stream specified by the argument.void
Outputs a printable representation of the receiver's walkback on the writer specified by the argument.void
setStackTrace
(StackTraceElement[] trace) Sets the array of StackTraceElements.toString()
Answers a string containing a concise, human-readable description of the receiver.
-
Constructor Details
-
Throwable
public Throwable()Constructs a new instance of this class with its walkback filled in. -
Throwable
Constructs a new instance of this class with its walkback and message filled in.- Parameters:
detailMessage
- String The detail message for the exception.
-
Throwable
Constructs a new instance of this class with its walkback, message and cause filled in.- Parameters:
detailMessage
- String The detail message for the exception.throwable
- The cause of this Throwable
-
Throwable
Constructs a new instance of this class with its walkback and cause filled in.- Parameters:
throwable
- The cause of this Throwable
-
Throwable
protected Throwable(String detailMessage, Throwable throwable, boolean enableSuppression, boolean enableWritableStackTrace) Constructs a new instance of this class with its walkback, message and cause filled in. enableSuppression and enableWritableStackTrace are true by default in other constructors If enableSuppression is false, suppression is disabled, getSuppressed() returns a zero-length array and calls to addSuppressed(Throwable) have no effect. If enableWritableStackTrace is false, fillInStackTrace() will not be called within this constructor, stackTrace field will be set to null, subsequent calls to fillInStackTrace() and setStackTrace(StackTraceElement[]) will not set the stack trace, and getStackTrace() will return a zero length array.- Parameters:
detailMessage
- String The detail message for the exception.throwable
- The cause of this ThrowableenableSuppression
- boolean enable or disable suppressionenableWritableStackTrace
- boolean whether the stack trace is writable- Since:
- 1.7
-
-
Method Details
-
fillInStackTrace
Record in the receiver a walkback from the point where this message was sent. The message is public so that code which catches a throwable and then re-throws it can adjust the walkback to represent the location where the exception was re-thrown.- Returns:
- the receiver
-
getMessage
Answers the extra information message which was provided when the throwable was created. If no message was provided at creation time, then answer null.- Returns:
- String The receiver's message.
-
getLocalizedMessage
Answers the extra information message which was provided when the throwable was created. If no message was provided at creation time, then answer null. Subclasses may override this method to answer localized text for the message.- Returns:
- String The receiver's message.
-
getStackTrace
Answers an array of StackTraceElement. Each StackTraceElement represents a entry on the stack.- Returns:
- an array of StackTraceElement representing the stack
-
setStackTrace
Sets the array of StackTraceElements. Each StackTraceElement represents a entry on the stack. A copy of this array will be returned by getStackTrace() and printed by printStackTrace().- Parameters:
trace
- The array of StackTraceElement
-
printStackTrace
public void printStackTrace()Outputs a printable representation of the receiver's walkback on the System.err stream. -
printStackTrace
Outputs a printable representation of the receiver's walkback on the stream specified by the argument.- Parameters:
err
- PrintStream The stream to write the walkback on.
-
printStackTrace
Outputs a printable representation of the receiver's walkback on the writer specified by the argument.- Parameters:
err
- PrintWriter The writer to write the walkback on.
-
toString
Answers a string containing a concise, human-readable description of the receiver. -
initCause
Initialize the cause of the receiver. The cause cannot be reassigned.- Parameters:
throwable
- The cause of this Throwable- Returns:
- the receiver.
- Throws:
IllegalArgumentException
- when the cause is the receiverIllegalStateException
- when the cause has already been initialized
-
getCause
Answers the cause of this Throwable, or null if there is no cause.- Returns:
- Throwable The receiver's cause.
-
addSuppressed
The specified exception is going to be suppressed in order to give priority to this exception (primary exception) and to be appended to the list of suppressed exceptions. This method is typically called by the automatically generated code from the try-with-resources statement.- Parameters:
exception
- Throwable an exception to be suppressed and added to the list of suppressed exceptions- Throws:
IllegalArgumentException
- if exception is this throwable, can't suppress itselfNullPointerException
- if exception is null and there is an exception suppressed before- Since:
- 1.7
-
getSuppressed
Returns an array of exceptions suppressed, typically by the automatically generated code from the try-with-resources statement, in order to give priority to this exception (primary exception).- Returns:
- an array of exceptions representing all exceptions suppressed to give priority to this exception (primary exception)
- Since:
- 1.7
-