Java Reference
In-Depth Information
Listing 9-13 demonstrates how to get to the stack frames of a thread. A Throwable object captures the stack of
the thread at the point it is created. If you have a Throwable object and want to capture the snapshot of the stack of a
thread at a different point where the Throwable object was created, you can call the fillInStackTrace() method of
the Throwable class. It captures the current state of stack for the current thread at the point you call this method.
Listing 9-13. A Sample Program That Prints the Details of the Stack Frames of a Thread
// StackFrameTest.java
package com.jdojo.exception;
public class StackFrameTest {
public static void main(String[] args) {
m1();
}
public static void m1() {
m2();
}
public static void m2() {
m3();
}
public static void m3() {
// Create a Throwable object that will hold the stack state
// at this point for the thread that executes the following statement
Throwable t = new Throwable();
// Get the stack trace elements
StackTraceElement[] frames = t.getStackTrace();
// Print details about the stack frames
printStackDetails(frames);
}
public static void printStackDetails(StackTraceElement[] frames) {
System.out.println("Frame count: " + frames.length);
for (int i = 0; i < frames.length; i++) {
// Get frame details
int frameIndex = i; // i = 0 means top frame
String fileName = frames[i].getFileName();
String className = frames[i].getClassName();
String methodName = frames[i].getMethodName();
int lineNumber = frames[i].getLineNumber();
// Print frame details
System.out.println("Frame Index: " + frameIndex);
System.out.println("File Name: " + fileName);
System.out.println("Class Name: " + className);
System.out.println("Method Name: " + methodName);
 
Search WWH ::




Custom Search