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 2023-08-21 11:01 Hello-World3 阅读(52) 评论(0) 编辑 收藏 举报