|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Throwable
public class Throwable
The Throwable
class is the superclass of all errors
and exceptions in the Java language. Only objects that are
instances of this class (or of one of its subclasses) are thrown
by the Java Virtual Machine or can be thrown by the Java
throw
statement. Similarly, only this class or one of
its subclasses can be the argument type in a catch
clause.
Instances of two subclasses, Error
and
Exception
, are conventionally used to indicate
that exceptional situations have occurred. Typically, these instances
are freshly created in the context of the exceptional situation so
as to include relevant information (such as stack trace data).
By convention, class Throwable
and its subclasses have
two constructors, one that takes no arguments and one that takes a
String
argument that can be used to produce an error
message.
A Throwable
class contains a snapshot of the
execution stack of its thread at the time it was created. It can
also contain a message string that gives more information about
the error.
Here is one example of catching an exception:
RIM Implementation Notetry { int a[] = new int[2]; a[4]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("exception: " + e.getMessage()); e.printStackTrace(); }
Only uncaught exceptions have stack traces. The VM checks the current catch stack and if it finds anything that will catch the exception, it eliminates the stack trace to save time and memory. Any code in the current stack such as catch (Exception e) eliminates the stack trace.
If the exception is never caught, then the stack trace is generated.The stack trace is also generated if there is code such as catch (Throwable t).
Constructor Summary | ||
---|---|---|
Throwable()
Constructs a new Throwable with null as
its error message string. |
||
Throwable(String message)
Constructs a new Throwable with the specified error
message. |
Method Summary | ||
---|---|---|
String |
getMessage()
Returns the error message string of this throwable object. |
|
void |
printStackTrace()
Prints this Throwable and its backtrace to the
standard error stream. |
|
String |
toString()
Returns a short description of this throwable object. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Throwable()
Throwable
with null
as
its error message string. Also, the method
public Throwable(String message)
Throwable
with the specified error
message.
message
- the error message. The error message is saved for
later retrieval by the Throwable.getMessage()
method.Method Detail |
---|
public String getMessage()
Throwable
object if it was created
with an
error message string; or null
if it was
created
with no error message.public String toString()
Throwable
object was
created
with an error message string,
then the result is the concatenation of three strings:
Throwable.getMessage()
method for this object
Throwable
object was created
with no error message string, then the name of the actual class of
this object is returned.
toString
in class Object
Throwable
.public void printStackTrace()
Throwable
and its backtrace to the
standard error stream.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal