Throwable class is the superclass of all errors and exceptions in the Java language.
More...
#include <java_lang_Throwable.h>
Inheritance diagram for java::lang::Throwable:


Public Types | |
| enum | { xmogDefaultFlags = xmog_base::GLOBAL, xmogMajorVersion = 3, xmogMinorVersion = 2, xmogPatchVersion = 9, xmogBuildNumber = 2047 } |
|
typedef ::xmog_java_array_template< ::java::lang::Throwable > | array1D |
|
typedef ::xmog_java_array_template< ::xmog_java_array_template< ::java::lang::Throwable > > | array2D |
Public Member Functions | |
| virtual ::java::lang::Throwable | fillInStackTrace (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Fills in the execution stack trace. | |
| virtual ::java::lang::Throwable | getCause (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
Returns the cause of this throwable or null if the cause is nonexistent or unknown. | |
| virtual ::java::lang::String | getLocalizedMessage (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Creates a localized description of this throwable. | |
| virtual ::java::lang::String | getMessage (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Returns the detail message string of this throwable. | |
| virtual ::java::lang::Throwable | initCause (const ::java::lang::Throwable &cause,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Initializes the cause of this throwable to the specified value. | |
| virtual void | printStackTrace (const ::java::io::PrintWriter &s,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Prints this throwable and its backtrace to the specified print writer. | |
| virtual void | printStackTrace (const ::java::io::PrintStream &s,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Prints this throwable and its backtrace to the specified print stream. | |
| virtual void | printStackTrace (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Prints this throwable and its backtrace to the standard error stream. | |
| Throwable (const ::java::lang::Throwable &cause,::xmog_dummy _use_java_ctor,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) | |
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). | |
| Throwable (const ::java::lang::String &message, const ::java::lang::Throwable &cause,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) | |
| Constructs a new throwable with the specified detail message and cause. | |
| Throwable (::xmog_dummy _use_java_ctor,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) | |
Constructs a new throwable with null as its detail message. | |
| Throwable (const ::java::lang::String &message,::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) | |
| Constructs a new throwable with the specified detail message. | |
| virtual ::java::lang::String | toString (::xmog_localenv *p_xenv=NULL, xmog_flags f_xflags=xmogDefaultFlags) |
| Returns a short description of this throwable. | |
Throwable class is the superclass of all errors and exceptions in the Java language.
Only objects that are instances of this class (or 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, and , 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).
A throwable 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. Finally, it can contain a cause: another throwable that caused this throwable to get thrown. The cause facility is new in release 1.4. It is also known as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.
One reason that a throwable may have a cause is that the class that throws it is built atop a lower layered abstraction, and an operation on the upper layer fails due to a failure in the lower layer. It would be bad design to let the throwable thrown by the lower layer propagate outward, as it is generally unrelated to the abstraction provided by the upper layer. Further, doing so would tie the API of the upper layer to the details of its implementation, assuming the lower layer's exception was a checked exception. Throwing a "wrapped exception" (i.e., an exception containing a cause) allows the upper layer to communicate the details of the failure to its caller without incurring either of these shortcomings. It preserves the flexibility to change the implementation of the upper layer without changing its API (in particular, the set of exceptions thrown by its methods).
A second reason that a throwable may have a cause is that the method that throws it must conform to a general-purpose interface that does not permit the method to throw the cause directly. For example, suppose a persistent collection conforms to the Collection interface, and that its persistence is implemented atop java.io. Suppose the internals of the put method can throw an IOException. The implementation can communicate the details of the IOException to its caller while conforming to the Collection interface by wrapping the IOException in an appropriate unchecked exception. (The specification for the persistent collection should indicate that it is capable of throwing such exceptions.)
A cause can be associated with a throwable in two ways: via a constructor that takes the cause as an argument, or via the method. New throwable classes that wish to allow causes to be associated with them should provide constructors that take a cause and delegate (perhaps indirectly) to one of the Throwable constructors that takes a cause. For example:
try { lowLevelOp(); } catch (LowLevelException le) { throw new HighLevelException(le); // Chaining-aware constructor }
Because the initCause method is public, it allows a cause to be associated with any throwable, even a "legacy throwable" whose implementation predates the addition of the exception chaining mechanism to Throwable. For example:
try { lowLevelOp(); } catch (LowLevelException le) { throw (HighLevelException) new HighLevelException().initCause(le); // Legacy constructor }
Prior to release 1.4, there were many throwables that had their own non-standard exception chaining mechanisms ( , , , , , , and ). As of release 1.4, all of these throwables have been retrofitted to use the standard exception chaining mechanism, while continuing to implement their "legacy" chaining mechanisms for compatibility.
Further, as of release 1.4, many general purpose Throwable classes (for example , , ) have been retrofitted with constructors that take a cause. This was not strictly necessary, due to the existence of the initCause method, but it is more convenient and expressive to delegate to a constructor that takes a cause.
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 a detail message. Further, those subclasses that might likely have a cause associated with them should have two more constructors, one that takes a Throwable (the cause), and one that takes a String (the detail message) and a Throwable (the cause).
Also introduced in release 1.4 is the method, which allows programmatic access to the stack trace information that was previously available only in text form, via the various forms of the method. This information has been added to the serialized representation of this class so getStackTrace and printStackTrace will operate properly on a throwable that was obtained by deserialization.
Josh Bloch (Added exception chaining and programmatic access to stack trace in 1.4.)
|
||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||||||
|
Constructs a new throwable with the specified detail message and cause.
Note that the detail message associated with The method is called to initialize the stack trace data in the newly created throwable.
|
|
||||||||||||||||||||
|
Constructs a new throwable with the specified cause and a detail message of This constructor is useful for throwables that are little more than wrappers for other throwables (for example, ). The method is called to initialize the stack trace data in the newly created throwable.
|
|
||||||||||||
|
Fills in the execution stack trace.
This method records within this
|
|
||||||||||||
|
Returns the cause of this throwable or (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
Reimplemented in java::lang::reflect::InvocationTargetException, and javax::naming::NamingException. |
|
||||||||||||
|
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
|
|
||||||||||||
|
Returns the detail message string of this throwable.
|
|
||||||||||||||||
|
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 or , this method cannot be called even once.
Reimplemented in javax::naming::NamingException. |
|
||||||||||||||||
|
Prints this throwable and its backtrace to the specified print writer.
|
|
||||||||||||||||
|
Prints this throwable and its backtrace to the specified print stream.
|
|
||||||||||||
|
Prints this throwable and its backtrace to the standard error stream.
This method prints a stack trace for this
java.lang.NullPointerException
at MyClass.mash(MyClass.java:9)
at MyClass.crunch(MyClass.java:6)
at MyClass.main(MyClass.java:3)
</blockquote> 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
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 { }
|
|
||||||||||||
|
Returns a short description of this throwable.
If this
Throwable object was created with a null detail message string, then the name of the actual class of this object is returned.
Reimplemented from java::lang::Object. Reimplemented in javax::naming::NamingException. |
1.4.1