
打印函数调用堆栈
1
2
3
4
5
6
7
8
9
10
11
|
Throwable ex = new Throwable();
StackTraceElement[] stackElements = ex.getStackTrace();
if (stackElements != null) {
for (int i = 0; i < stackElements.length; i++) {
System.out.print(stackElements[i].getClassName() + " ");
System.out.print(stackElements[i].getFileName()+ " ");
System.out.print(stackElements[i].getLineNumber()+ " ");
System.out.println(stackElements[i].getMethodName()+ " ");
System.out.println("-----------------------------------");
}
}
|
1
|
Exception e = new Exception("this is a log").printStackTracje();
|
- Thread.currentThread().getStackTrace()
1
2
3
4
5
6
7
8
9
10
|
StackTraceElement[] stackElements = Thread.currentThread().getStackTrace();
if (stackElements != null) {
for (int i = 0; i < stackElements.length; i++) {
System.out.print(stackElements[i].getClassName() + " ");
System.out.print(stackElements[i].getFileName()+ " ");
System.out.print(stackElements[i].getLineNumber()+ " ");
System.out.println(stackElements[i].getMethodName()+ " ");
System.out.println("-----------------------------------");
}
}
|
1
|
org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
|
第1、3种方法都是遍历输出StackTraceElement数组,第2种更适合调试时使用,第4种需要借助三方包
近期评论