堆栈跟踪:方法调用过程的列表。
public class Check { private void before() { run(); } private void run() { after(); } private void after() { end(); } private void end() { StackTraceElement[] elements = Thread.currentThread().getStackTrace(); for (StackTraceElement element : elements) { System.out.println(element.getMethodName()+" : "+ element.getLineNumber()); } } public static void main(String[] args) { new Check().before(); } }
执行结果:
getStackTrace : 1559 end : 19 after : 14 run : 10 before : 6 main : 28