Name

Throwable

Synopsis

This is the root class of the Java exception and error hierarchy. All exceptions and errors are subclasses of Throwable. The getMessage( ) method retrieves any error message associated with the exception or error. The default implemenation of getLocalizedMessage( ) simply calls getMessage( ), but subclasses may override this method to return an error message that has been localized for the default locale.

It is often the case that an Exception or Error is generated as a direct result of some other exception or error, perhaps one thrown by a lower-level API. As of Java 1.4 and later, all Throwable objects may have a “cause” which specifies the Throwable that caused this one. If there is a cause, pass it to the Throwable( ) constructor, or to the initCause( ) method. When you catch a Throwable object, you can obtain the Throwable that caused it, if any, with getCause( ).

Every Throwable object has information about the execution stack associated with it. This information is initialized when the Throwable object is created. If the object will be thrown somewhere other than where it was created, or if it caught and will be re-thrown, you can use fillInStackTrace( ) to capture the current execution stack before throwing it. printStackTrace( ) prints a textual representation of the stack to the specified PrintWriter, PrintStream, or to the System.err stream. In Java 1.4, you can also obtain this information with getStackTrace( ) which returns an array of StackTraceElement ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.