Throwable |
getCause()
public Throwable getCause()
- Returns the cause of this throwable or
null if the
cause is nonexistent or unknown. (The cause is the throwable that
caused this throwable to get thrown.)
This implementation returns the cause that was supplied via one of
the constructors requiring a Throwable, or that was set after
creation with the Throwable.initCause(Throwable) method. While it is
typically unnecessary to override this method, a subclass can override
it to return a cause set by some other means. This is appropriate for
a "legacy chained throwable" that predates the addition of chained
exceptions to Throwable. Note that it is not
necessary to override any of the PrintStackTrace methods,
all of which invoke the getCause method to determine the
cause of a throwable.
- Returns:
- the cause of this throwable or
null if the
cause is nonexistent or unknown. - Since:
- 1.4
String |
getLocalizedMessage()
public String getLocalizedMessage()
- Creates a localized description of this throwable.
Subclasses may override this method in order to produce a
locale-specific message. For subclasses that do not override this
method, the default implementation returns the same result as
getMessage().
- Returns:
- The localized description of this throwable.
- Since:
- JDK1.1
String |
getMessage()
public String getMessage()
- Returns the detail message string of this throwable.
- Returns:
- the detail message string of this Throwable instance
(which may be null).
StackTraceElement[] |
getStackTrace()
public StackTraceElement[] getStackTrace()
- Provides programmatic access to the stack trace information printed by
Throwable.printStackTrace(). Returns an array of stack trace elements,
each representing one stack frame. The zeroth element of the array
(assuming the array's length is non-zero) represents the top of the
stack, which is the last method invocation in the sequence. Typically,
this is the point at which this throwable was created and thrown.
The last element of the array (assuming the array's length is non-zero)
represents the bottom of the stack, which is the first method invocation
in the sequence.
Some virtual machines may, under some circumstances, omit one
or more stack frames from the stack trace. In the extreme case,
a virtual machine that has no stack trace information concerning
this throwable is permitted to return a zero-length array from this
method. Generally speaking, the array returned by this method will
contain one element for every frame that would be printed by
printStackTrace.
- Returns:
- an array of stack trace elements representing the stack trace
pertaining to this throwable.
- Since:
- 1.4
Throwable |
initCause(Throwable cause)
public Throwable initCause(Throwable cause)
- Initializes the cause of this throwable to the specified value.
(The cause is the throwable that caused this throwable to get thrown.)
This method can be called at most once. It is generally called from
within the constructor, or immediately after creating the
throwable. If this throwable was created
with Throwable.Throwable(Throwable) or
Throwable.Throwable(String,Throwable), this method cannot be called
even once.
- Parameters:
cause - the cause (which is saved for later retrieval by the
Throwable.getCause() method). (A null value is
permitted, and indicates that the cause is nonexistent or
unknown.)
- Returns:
- a reference to this
Throwable instance.
- Throws:
IllegalArgumentException - if cause is this
throwable. (A throwable cannot be its own cause.)
IllegalStateException - if this throwable was
created with Throwable.Throwable(Throwable) or
Throwable.Throwable(String,Throwable), or this method has already
been called on this throwable.- Since:
- 1.4
void |
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 Throwable.toString() method for
this object. Remaining lines represent data previously recorded by
the method Throwable.fillInStackTrace(). The format of this
information depends on the implementation, but the following
example may be regarded as typical:
java.lang.NullPointerException
at MyClass.mash(MyClass.java:9)
at MyClass.crunch(MyClass.java:6)
at MyClass.main(MyClass.java:3)
This example was produced by running the program:
class MyClass {
public static void main(String[] args) {
crunch(null);
}
static void crunch(int[] a) {
mash(a);
}
static void mash(int[] b) {
System.out.println(b[0]);
}
}
The backtrace for a throwable with an initialized, non-null cause
should generally include the backtrace for the cause. The format
of this information depends on the implementation, but the following
example may be regarded as typical:
HighLevelException: MidLevelException: LowLevelException
at Junk.a(Junk.java:13)
at Junk.main(Junk.java:4)
Caused by: MidLevelException: LowLevelException
at Junk.c(Junk.java:23)
at Junk.b(Junk.java:17)
at Junk.a(Junk.java:11)
... 1 more
Caused by: LowLevelException
at Junk.e(Junk.java:30)
at Junk.d(Junk.java:27)
at Junk.c(Junk.java:21)
... 3 more
Note the presence of lines containing the characters "...".
These lines indicate that the remainder of the stack trace for this
exception matches the indicated number of frames from the bottom of the
stack trace of the exception that was caused by this exception (the
"enclosing" exception). This shorthand can greatly reduce the length
of the output in the common case where a wrapped exception is thrown
from same method as the "causative exception" is caught. The above
example was produced by running the program:
public class Junk {
public static void main(String args[]) {
try {
a();
} catch(HighLevelException e) {
e.printStackTrace();
}
}
static void a() throws HighLevelException {
try {
b();
} catch(MidLevelException e) {
throw new HighLevelException(e);
}
}
static void b() throws MidLevelException {
c();
}
static void c() throws MidLevelException {
try {
d();
} catch(LowLevelException e) {
throw new MidLevelException(e);
}
}
static void d() throws LowLevelException {
e();
}
static void e() throws LowLevelException {
throw new LowLevelException();
}
}
class HighLevelException extends Exception {
HighLevelException(Throwable cause) { super(cause); }
}
class MidLevelException extends Exception {
MidLevelException(Throwable cause) { super(cause); }
}
class LowLevelException extends Exception {
}
void |
printStackTrace(PrintStream s)
public void printStackTrace(PrintStream s)
- Prints this throwable and its backtrace to the specified print stream.
- Parameters:
s - PrintStream to use for output
void |
printStackTrace(PrintWriter s)
public void printStackTrace(PrintWriter s)
- Prints this throwable and its backtrace to the specified
print writer.
- Parameters:
s - PrintWriter to use for output- Since:
- JDK1.1
void |
setStackTrace(StackTraceElement[] stackTrace)
public void setStackTrace(StackTraceElement[] stackTrace)
- Sets the stack trace elements that will be returned by
Throwable.getStackTrace() and printed by Throwable.printStackTrace()
and related methods.
This method, which is designed for use by RPC frameworks and other
advanced systems, allows the client to override the default
stack trace that is either generated by Throwable.fillInStackTrace()
when a throwable is constructed or deserialized when a throwable is
read from a serialization stream.
- Parameters:
stackTrace - the stack trace elements to be associated with
this Throwable. The specified array is copied by this
call; changes in the specified array after the method invocation
returns will have no affect on this Throwable's stack
trace.
- Throws:
NullPointerException - if stackTrace is
null, or if any of the elements of
stackTrace are null- Since:
- 1.4
String |
toString()
public String toString()
- Returns a short description of this throwable.
If this
Throwable object was created with a non-null detail
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
Throwable.getMessage() method for this object
If this Throwable object was created with a null
detail message string, then the name of the actual class of this object
is returned.
- Overrides:
toString in class Object
- Returns:
- a string representation of this throwable.
| | | | | | | | | |