Java Reference
In-Depth Information
Obtaining Data from an Exception Object
All exceptions derive from class
Throwable
, which has a
printStackTrace
method that
outputs to the standard error stream the
stack trace
(discussed in Section 11.2). Often this
is helpful in testing and debugging. Class
Throwable
also provides a
getStackTrace
meth-
od that retrieves the stack-trace information that might be printed by
printStackTrace
.
Class
Throwable
's
getMessage
method returns the descriptive string stored in an exception.
Error-Prevention Tip 11.7
An exception that's not caught in an application causes Java's
default exception handler
to
run. This displays the name of the exception, a descriptive message that indicates the problem
that occurred and a complete execution stack trace. In an application with a single thread
of execution, the application terminates. In an application with multiple threads, the thread
that caused the exception terminates. We discuss multithreading in Chapter 23.
Error-Prevention Tip 11.8
Throwable
method
toString
(inherited by all
Throwable
subclasses) returns a
String
containing the name of the exception's class and a descriptive message.
The
catch
handler in Fig. 11.6 (lines 12-31) demonstrates
getMessage
,
print-
StackTrace
and
getStackTrace
. If we wanted to output the stack-trace information to
streams other than the standard error stream, we could use the information returned from
getStackTrace
and output it to another stream or use one of the overloaded versions of
method
printStackTrace
. Sending data to other streams is discussed in Chapter 15.
Line 14 invokes the exception's
getMessage
method to get the
exception description
.
Line 15 invokes the exception's
printStackTrace
method to output the
stack trace
that
indicates where the exception occurred. Line 18 invokes the exception's
getStackTrace
method to obtain the stack-trace information as an array of
StackTraceElement
objects.
Lines 24-30 get each
StackTraceElement
in the array and invoke its methods
getClass-
Name
,
getFileName
,
getLineNumber
and
getMethodName
to get the class name, filename,
line number and method name, respectively, for that
StackTraceElement
. Each
Stack-
TraceElement
represents
one
method call on the
method-call stack
.
The program's output shows that output of
printStackTrace
follows the pattern:
class-
Name
.
methodName
(
fileName
:
lineNumber
), where
className
,
methodName
and
fileName
indicate the names of the class, method and file in which the exception occurred, respec-
tively, and the
lineNumber
indicates where in the file the exception occurred. You saw this
in the output for Fig. 11.2. Method
getStackTrace
enables custom processing of the excep-
tion information. Compare the output of
printStackTrace
with the output created from
the
StackTraceElement
s to see that both contain the same stack-trace information.
Software Engineering Observation 11.11
Occasionally, you might want to ignore an exception by writing a
catch
handler with an
empty body. Before doing so, ensure that the exception doesn't indicate a condition that
code higher up the stack might want to know about or recover from.
Sometimes a method responds to an exception by throwing a different exception type
that's specific to the current application. If a
catch
block throws a new exception, the orig-