Processing Stack Trace Data in Java

The first line of output represents the type of one Throwable
object, and the message encapsulated in that object.

The second line of output represents one stack frame that existed at
the point in time that the Throwable object was created and thrown.
If the stack contained more than one frame, other lines similar to the
second line would be produced, one for each stack frame.

From this output, we can see that three separate Throwable objects
were thrown in the chain of exceptions. The first object that was
thrown and the state of the stack at the time it was created and thrown
are identified by the boldface text at the bottom of Figure 3.

As shown by the top boldface line of text, the first object that was
thrown was of type NewEx03. The message reads: "Thrown
from meth04".

Five frames on the stack

There were five frames on the stack at the time the object was thrown.
According to Sun:

"All stack frames except for the one at the top of the stack
represent a method invocation. The frame at the top of the stack represents
the execution point at which the stack trace was generated. Typically,
this is the point at which the throwable corresponding to the stack trace
was created."

The frame at the bottom of the stack represents the main method
of the StackTr01 class.

The frame at the top of the stack represents the method named meth04
of the class named Class01. This is the method that threw
the first exception. The line number of 112"represents
the execution point at which the stack trace was generated."

When was the Throwable object created?

The stack trace data indicates that the object was created at line 112
in the source code for the program. Lines 112 and 113 in the source
code read as shown in Figure 4.

112 throw new NewEx03(
113 "Thrown from meth04");
Figure 4

(Although the statement that created the Throwable
object is spread across two lines, it begins on line 112.)

Method invocations

As indicated in the earlier quotation from Sun, the remaining line numbers
in the boldface text represent method invocations in the methods and classes
indicated.

For example, the statements corresponding to the line numbers in the
last four lines in Figure 3 are shown in Figure 5. As you can see,
each of these statements is a method invocation.

At this point, I recommend that you compare Figure 3 with Figure 1.
Figure 1 shows the output from the printStackTrace method for this
program. Figure 3 shows my programmatic formatting of stack trace
data for the same program. Except for the fact that some of the data
is missing in Figure 1, you should be able to match up each of the data
elements in Figure 3 with the corresponding data elements in Figure 1.

Programmatic handling of stack trace data

Now you know how to get stack trace information and encapsulate it in
an object suitable for processing under program control. You know
how to serialize that data so that it can be written in a disk file or
transmitted across a network. You know how to read the serialized
byte stream and reconstruct the original object containing the stack trace
data.

Equally important, you also know how to interpret the stack trace data
that is available in this fashion. Obviously, you can't write code
to process the stack trace data, and make decisions on the basis of that
data, unless you know how to interpret it.

Run the Program

If you haven't already done so, I encourage you to copy the code from Listing
21 into your text editor, compile it, and execute it. Experiment
with it, making changes, and observing the results of your changes.

Remember, however, that you must be running Java version 1.4 or later
to compile and execute this program.

Complete Program Listing

A complete listing of the program discussed in this lesson is shown in
Listing 21.

Copyright 2002, Richard G. Baldwin. Reproduction in whole or in
part in any form or medium without express written permission from Richard
Baldwin is prohibited.

About the author

Richard G. Baldwin
is a college professor (at Austin Community College in Austin, TX) and
private consultant whose primary focus is a combination of Java, C#, and
XML. In addition to the many platform and/or language independent benefits
of Java and C# applications, he believes that a combination of Java, C#,
and XML will become the primary driving force in the delivery of structured
information on the Web.

Richard has participated in numerous consulting projects, and he
frequently provides onsite training at the high-tech companies located
in and around Austin, Texas. He is the author of Baldwin's Programming
Tutorials,
which has gained a worldwide following among experienced and aspiring programmers.
He has also published articles in JavaPro magazine.

Richard holds an MSEE degree from Southern Methodist University and
has many years of experience in the application of computer technology
to real-world problems.