|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.4.2
java.lang
|
Constructor Summary | |
Throwable()
Constructs a new throwable with null as its detail message. |
|
Throwable(String message)
Constructs a new throwable with the specified detail message. |
|
Throwable(String message,
Throwable cause)
Constructs a new throwable with the specified detail message and cause. |
|
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). |
Method Summary | |
Throwable |
fillInStackTrace()
Fills in the execution stack trace. |
Throwable |
getCause()
Returns the cause of this throwable or null if the
cause is nonexistent or unknown. |
String |
getLocalizedMessage()
Creates a localized description of this throwable. |
String |
getMessage()
Returns the detail message string of this throwable. |
StackTraceElement[] |
getStackTrace()
Provides programmatic access to the stack trace information printed by printStackTrace() . |
Throwable |
initCause(Throwable cause)
Initializes the cause of this throwable to the specified value. |
void |
printStackTrace()
Prints this throwable and its backtrace to the standard error stream. |
void |
printStackTrace(PrintStream s)
Prints this throwable and its backtrace to the specified print stream. |
void |
printStackTrace(PrintWriter s)
Prints this throwable and its backtrace to the specified print writer. |
void |
setStackTrace(StackTraceElement[] stackTrace)
Sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace()
and related methods. |
String |
toString()
Returns a short description of this throwable. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public Throwable()
null
as its detail message.
The cause is not initialized, and may subsequently be initialized by a
call to initCause(java.lang.Throwable)
.
The fillInStackTrace()
method is called to initialize
the stack trace data in the newly created throwable.
public Throwable(String message)
initCause(java.lang.Throwable)
.
The fillInStackTrace()
method is called to initialize
the stack trace data in the newly created throwable.
message
- the detail message. The detail message is saved for
later retrieval by the getMessage()
method.public Throwable(String message, Throwable cause)
Note that the detail message associated with
cause
is not automatically incorporated in
this throwable's detail message.
The fillInStackTrace()
method is called to initialize
the stack trace data in the newly created throwable.
message
- the detail message (which is saved for later retrieval
by the getMessage()
method).cause
- the cause (which is saved for later retrieval by the
getCause()
method). (A null value is
permitted, and indicates that the cause is nonexistent or
unknown.)public Throwable(Throwable cause)
PrivilegedActionException
).
The fillInStackTrace()
method is called to initialize
the stack trace data in the newly created throwable.
cause
- the cause (which is saved for later retrieval by the
getCause()
method). (A null value is
permitted, and indicates that the cause is nonexistent or
unknown.)Method Detail |
public String getMessage()
public String getLocalizedMessage()
getMessage()
.
public Throwable getCause()
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 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.
null
if the
cause is nonexistent or unknown.public Throwable initCause(Throwable cause)
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)
or
Throwable(String,Throwable)
, this method cannot be called
even once.
cause
- the cause (which is saved for later retrieval by the
getCause()
method). (A null value is
permitted, and indicates that the cause is nonexistent or
unknown.)
Throwable
instance.
IllegalArgumentException
- if cause
is this
throwable. (A throwable cannot be its own cause.)
IllegalStateException
- if this throwable was
created with Throwable(Throwable)
or
Throwable(String,Throwable)
, or this method has already
been called on this throwable.public String toString()
Throwable
object was created with a non-null detail
message string, then the result is the concatenation of three strings:
getMessage()
method for this object
Throwable
object was created with a null
detail message string, then the name of the actual class of this object
is returned.
public void printStackTrace()
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 toString()
method for
this object. Remaining lines represent data previously recorded by
the method fillInStackTrace()
. The format of this
information depends on the implementation, but the following
example may be regarded as typical:
This example was produced by running the program:java.lang.NullPointerException at MyClass.mash(MyClass.java:9) at MyClass.crunch(MyClass.java:6) at MyClass.main(MyClass.java:3)
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 moreNote 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 { }
public void printStackTrace(PrintStream s)
s
- PrintStream
to use for outputpublic void printStackTrace(PrintWriter s)
s
- PrintWriter
to use for outputpublic Throwable fillInStackTrace()
Throwable
object information about the current state of
the stack frames for the current thread.
Throwable
instance.printStackTrace()
public StackTraceElement[] getStackTrace()
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.
public void setStackTrace(StackTraceElement[] stackTrace)
getStackTrace()
and printed by 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 fillInStackTrace()
when a throwable is constructed or deserialized when a throwable is
read from a serialization stream.
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.
NullPointerException
- if stackTrace
is
null
, or if any of the elements of
stackTrace
are null