How do stack traces get generated?

后端 未结 4 1901
盖世英雄少女心
盖世英雄少女心 2021-01-20 23:53

No single method in a program \"knows\" where it is on the stack. All it knows is its own little job, and it does that and returns. So when an Exception is thrown and a stack

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 00:14

    It comes from the Thread class that is running through the code.

    Thread.dumpStack();
    

    To see it you can just:

        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        for (int i=0; i < trace.length; i++)
            System.out.println("\tat " + trace[i]);
    

提交回复
热议问题