Java Reference
In-Depth Information
public String getMessage(). Returns a detailed message about the excep-
tion that has occurred. This message is initialized in the Throwable
constructor.
public Throwable getCause(). Returns the cause of the exception as rep-
resented by a Throwable object. This cause is initialized using either one
of the Throwable constructors or the initCause() method.
public String toString(). Returns the name of the class concatenated with
the result of getMessage().
public void printStackTrace(). Prints the result of toString() along with
the stack trace to System.err, the error output stream. (System.err is the
command prompt for your Java programs running on Windows.) This
method is overloaded for sending the stack trace to an output stream
that you specify.
public StackTraceElement [] getStackTrace(). Returns an array contain-
ing each element on the stack trace. The element at index 0 represents
the top of the call stack, and the last element in the array represents the
method at the bottom of the call stack. This method allows your applica-
tion to programmatically iterate through each line of the call stack.
public Throwable fillInStackTrace(). Fills the stack trace of this Throw-
able object with the current stack trace, adding to any previous informa-
tion in the stack trace.
The methods in Throwable are designed to assist you in determining how
and where the problem occurred. In the next section, we will discuss how an
exception is caught using a try/catch block.
Catching Exceptions
A method catches an exception using a combination of the try and catch key-
words. A try/catch block is placed around the code that might generate an
exception. Code within a try/catch block is referred to as protected code, and
the syntax for using try/catch looks like the following:
try
{
//Protected code
}catch( ExceptionName e1)
{
//Catch block
}
A catch statement involves declaring the type of exception you are trying to
catch. If an exception occurs in protected code, the catch block (or blocks) that
Search WWH ::




Custom Search