Java Reference
In-Depth Information
can produce messages that are a bit more informative than those you've used so far in our examples. For one
thing, I have totally ignored the exception object that is passed to the catch block.
The exception object that is passed to a catch block can provide additional information about the nature
of the problem that originated it. To understand more about this, let's first look at the members of the base
class for exceptions Throwable because these are inherited by all exception classes and are therefore con-
tained in every exception object that is thrown.
The Throwable Class
The Throwable class is the class from which all Java exception classes are derived — that is, every excep-
tion object contains the methods defined in this class. The Throwable class has five constructors:
Throwable() creates an object with no detail message.
Throwable(String message) create an object containing message as the message.
Throwable(String message, Throwable cause) creates an object containing message as the
message and a second Throwable object, cause , specifying the cause of the exception.
Throwable(String message, Throwable cause, boolean suppress, boolean stack-
Trace) creates an object containing message as the message and a second Throwable object,
cause , specifying the cause of the exception. If suppress is true , it allows exception to be sup-
pressed in order to deliver this exception. If stackTrace is true , recording of the stack trace is
enabled.
Throwable(Throwable cause) creates an object with the message cause.toString() if cause
is not null , and cause as the cause of the exception.
The constructors with Throwable parameters provide the basis for storing a reference to one exception
inside another. The cause reference can be obtained by calling getCause() for a Throwable object. This
allows exceptions to be chained, so when one exception has been thrown, you can create another exception
that provides more information about the problem and record within it a reference to the original exception
that caused the new exception to be thrown. This ensures that information available from the original excep-
tion is not lost. You'll see that you can define you own exception classes to allow exception chaining.
Objects of type Throwable can contain the following information:
• A message, which I have just referred to as being initialized by a constructor.
• A Throwable object identifying the cause of the exception.
• A record of the execution stack at the time the object was created.
• A record of exceptions suppressed in order to deliver this exception.
The execution stack keeps track of all the methods that are in execution at any given instant. It provides
the means whereby executing a return gets back to the calling point for a method. The record of the exe-
cution stack that is stored in the exception object consists of the line number in the source code where the
exception originates followed by a trace of the method calls that immediately precede the point at which the
exception occurs. This is made up of the fully qualified name for each of the methods called, plus the line
number in the source file where each method call occurs. The method calls are in sequence with the most
recent method call appearing first. This helps you to understand how this point in the program was reached.
The Throwable class has the following public methods that enable you to access the message, the cause,
and the stack trace as shown in Table 7-2 .
TABLE 7-2 : Throwable Class Public Methods
 
 
Search WWH ::




Custom Search