Java调试汇总—栈回溯

1. 打印栈回溯

(1) 直接使用 new RuntimeException("stack_dump").printStackTrace();

复制代码
 1 public class Outer {
 2     private int x = 10;
 3     
 4     public void print() {
 5         Inner inner = new Inner();
 6         inner.print();
 7     }
 8     class Inner {
 9         private int x = 20;
10         public void print() {
11             System.out.println("内部类的x:" + x);
12             System.out.println("外部类的x:"+ Outer.this.x);
13             new RuntimeException("stack_dump").printStackTrace(); //打印栈回溯
14         }
15     }
16     
17     public static void main(String args[]) {
18         Outer o = new Outer();
19         o.print();
20     }
21 }
22 
23 /*
24 内部类的x:20
25 外部类的x:10
26 java.lang.RuntimeException: stack_dump
27         at Outer$Inner.print(Outer.java:13)
28         at Outer.print(Outer.java:6)
29         at Outer.main(Outer.java:19)
30 */
复制代码

 

(2) 若有异常,也可以使用 e.printStackTrace() 进行打印

try { 
...
} catch (Exception e) {
    e.printStackTrace();
}

 

posted on   Hello-World3  阅读(59)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2017-08-21 谁是罪犯问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示