public abstract class StaticCheckedException extends Exception
Modifier | Constructor and Description |
---|---|
protected |
StaticCheckedException()
Enable this class to be extended.
|
protected |
StaticCheckedException(String message)
Enable this class to be extended.
|
Modifier and Type | Method and Description |
---|---|
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
Throwable.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 stream)
Prints this throwable and its backtrace to the specified print stream.
|
void |
printStackTrace(PrintWriter writer)
Prints this throwable and its backtrace to the specified
print writer.
|
void |
setStackTrace(StackTraceElement[] new_stackTrace)
Sets the stack trace elements that will be returned by
Throwable.getStackTrace() and printed by Throwable.printStackTrace()
and related methods. |
addSuppressed, getSuppressed, toString
protected StaticCheckedException()
protected StaticCheckedException(String message)
message
- Text to descript the exception.public String getMessage()
Throwable
getMessage
in class Throwable
Throwable
instance
(which may be null
).public String getLocalizedMessage()
Throwable
getMessage()
.getLocalizedMessage
in class Throwable
public Throwable initCause(Throwable cause)
Throwable
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.
An example of using this method on a legacy throwable type without other support for setting the cause is:
try { lowLevelOp(); } catch (LowLevelException le) { throw (HighLevelException) new HighLevelException().initCause(le); // Legacy constructor }
initCause
in class Throwable
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.)Throwable
instance.public Throwable getCause()
Throwable
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.
public Throwable fillInStackTrace()
Throwable
Throwable
object information about the current state of
the stack frames for the current thread.
If the stack trace of this Throwable
is not
writable, calling this method has no effect.
fillInStackTrace
in class Throwable
Throwable
instance.Throwable.printStackTrace()
public void setStackTrace(StackTraceElement[] new_stackTrace) throws NullPointerException
Throwable
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.
If the stack trace of this Throwable
is not
writable, calling this method has no effect other than
validating its argument.
setStackTrace
in class Throwable
NullPointerException
- if stackTrace
is
null
or if any of the elements of
stackTrace
are null
new_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.public StackTraceElement[] getStackTrace()
Throwable
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
. Writes to the returned array do not
affect future calls to this method.
getStackTrace
in class Throwable
public void printStackTrace()
Throwable
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:
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 { }As of release 7, the platform supports the notion of suppressed exceptions (in conjunction with the
try
-with-resources statement). Any exceptions that were
suppressed in order to deliver an exception are printed out
beneath the stack trace. The format of this information
depends on the implementation, but the following example may be
regarded as typical:
Exception in thread "main" java.lang.Exception: Something happened at Foo.bar(Foo.java:10) at Foo.main(Foo.java:5) Suppressed: Resource$CloseFailException: Resource ID = 0 at Resource.close(Resource.java:26) at Foo.bar(Foo.java:9) ... 1 moreNote that the "... n more" notation is used on suppressed exceptions just at it is used on causes. Unlike causes, suppressed exceptions are indented beyond their "containing exceptions."
An exception can have both a cause and one or more suppressed exceptions:
Exception in thread "main" java.lang.Exception: Main block at Foo3.main(Foo3.java:7) Suppressed: Resource$CloseFailException: Resource ID = 2 at Resource.close(Resource.java:26) at Foo3.main(Foo3.java:5) Suppressed: Resource$CloseFailException: Resource ID = 1 at Resource.close(Resource.java:26) at Foo3.main(Foo3.java:5) Caused by: java.lang.Exception: I did it at Foo3.main(Foo3.java:8)Likewise, a suppressed exception can have a cause:
Exception in thread "main" java.lang.Exception: Main block at Foo4.main(Foo4.java:6) Suppressed: Resource2$CloseFailException: Resource ID = 1 at Resource2.close(Resource2.java:20) at Foo4.main(Foo4.java:5) Caused by: java.lang.Exception: Rats, you caught me at Resource2$CloseFailException.<init>(Resource2.java:45) ... 2 more
printStackTrace
in class Throwable
public void printStackTrace(PrintStream stream)
Throwable
printStackTrace
in class Throwable
stream
- PrintStream
to use for outputpublic void printStackTrace(PrintWriter writer)
Throwable
printStackTrace
in class Throwable
writer
- PrintWriter
to use for outputaicas GmbH, Karlsruhe, Germany —www.aicas.com
Copyright © 2001-2019 aicas GmbH. All Rights Reserved.