System.out.println("The stackTraceElements length: "
+ stackTraceElements.length);
for (int i = 0; i < stackTraceElements.length; ++i) {
System.out.println("\n\n---- the " + i + " element ----");
System.out.println("toString: " + stackTraceElements[i].toString());
System.out.println("ClassName: "
+ stackTraceElements[i].getClassName());
System.out.println("FileName: "
+ stackTraceElements[i].getFileName());
System.out.println("LineNumber: "
+ stackTraceElements[i].getLineNumber());
System.out.println("MethodName: "
+ stackTraceElements[i].getMethodName());
}
//printStackInfos();
}
执行后输出:
The stackTraceElements length: 2
---- the 0 element ----
toString: java.lang.Thread.getStackTrace(Unknown Source)
ClassName: java.lang.Thread
FileName: null
LineNumber: -1
MethodName: getStackTrace
---- the 1 element ----
toString: com.mengdd.time.StackTest.main(StackTest.java:7)
ClassName: com.mengdd.time.StackTest
FileName: StackTest.java
LineNumber: 7
MethodName: main
System.out.println("\n\nCalled in printStackInfos() method!!!!!!!");
System.out.println("The stackTraceElements length: "
+ stackTraceElements.length);
for (int i = 0; i < stackTraceElements.length; ++i) {
System.out.println("\n\n---- the " + i + " element ----");
System.out.println("toString: " + stackTraceElements[i].toString());
System.out.println("ClassName: "
+ stackTraceElements[i].getClassName());
System.out.println("FileName: "
+ stackTraceElements[i].getFileName());
System.out.println("LineNumber: "
+ stackTraceElements[i].getLineNumber());
System.out.println("MethodName: "
+ stackTraceElements[i].getMethodName());
}
}
}
则输出结果如下:
The stackTraceElements length: 2
---- the 0 element ----
toString: java.lang.Thread.getStackTrace(Unknown Source)
ClassName: java.lang.Thread
FileName: null
LineNumber: -1
MethodName: getStackTrace
---- the 1 element ----
toString: com.mengdd.time.StackTest.main(StackTest.java:7)
ClassName: com.mengdd.time.StackTest
FileName: StackTest.java
LineNumber: 7
MethodName: main
Called in printStackInfos() method!!!!!!!
The stackTraceElements length: 3
---- the 0 element ----
toString: java.lang.Thread.getStackTrace(Unknown Source)
ClassName: java.lang.Thread
FileName: null
LineNumber: -1
MethodName: getStackTrace
---- the 1 element ----
toString: com.mengdd.time.StackTest.printStackInfos(StackTest.java:30)
ClassName: com.mengdd.time.StackTest
FileName: StackTest.java
LineNumber: 30
MethodName: printStackInfos
---- the 2 element ----
toString: com.mengdd.time.StackTest.main(StackTest.java:25)
ClassName: com.mengdd.time.StackTest
FileName: StackTest.java
LineNumber: 25
MethodName: main