【尚硅谷】idea实战教程-讲师:宋红康
生活是属于每个人自己的感受,不属于任何别人的看法
编好的程序在执行过程中如果出现错误,该如何查找或定位错误呢?
简单的代码直接就可以看出来,但如果代码比较复杂,就需要借助程序调试来查找错误了。
在运行编写好的程序时,可能出现的集中情况:
情况1:没有任何bug,程序执行正确。
===============如果出现如下三种情况,都有必要使用debug===============
情况2:运行后,出现了错误或异常信息,但是通过日志文件或控制台,显示了异常信息的位置。
情况3:运行后,得到了结果,但是结果不是预先设计的。
情况4:运行后,得到了结果,结果大多数情况是我们想要的,但是多次运行,会出现不是预先设计的结果在其中。
比如:多线程情况下,处理线程安全问题。
Debug(调试)程序步骤如下 :
1 、 添加断点
2 、 启动调试
3 、 单步执行
4 、 观察变量和执行流程,找到并解决问题。
在源代码文件中,在想要设置断点的代码行的前面的标记行处,单击鼠标左键就可以设置断点,在相同位置再次单击即可取消断点 。
IDEA提供多种方式来启动程序(Launch)的调试,分别是通过菜单(Run 一 > Debug)、图标 " 绿色臭虫" 等等
:显示执行点(ALT + F10):点击查看断点位置
:步过(F8):进入下一步,如果当前行断点是调用一个方法,则不进入当前方法体内
:步入(F7):进入下一步,如果当前行断点是调用一个自定义方法,则进入该方法体内
:强制步入(ALT + SHIFT + F7):进入下一步,如果当前行断点是调用一个核心类库方法 ,则进入该方法体内
:步出(Shift + F8):跳出当前方法体
:Run to Cursor(Alt + F9):直接跳到光标处继续调试
:恢复程序(F9):恢复程序运行,但如果该断点下面代码还有断点则停在下一个断点上
:停止(Ctrl + F2):结束调试
:查看断点(Ctrl + Shift + F8):看所有断点
:静音断点:使得当前代码后面所有的断点失效,一下执行到底。
断点打在代码所在行上,执行到此行时,会停下来。
public class Dubug01 {public static void main(String[] args) {//1int m = 10;int n = 20;System.out.println("m = " + m + ",n = " + n);swap(m, n);System.out.println("m = " + m + ",n = " + n);//2int[] arr = new int[]{1, 2, 3, 4, 5};System.out.println(arr);//地址值char[] arr1 = new char[]{'a', 'b', 'c'};System.out.println(arr1); //abc}public static void swap(int m, int n) {int temp = m;m = n;n = temp;}
}
断点设置在方法的签名上,默认当进入时,断点可以被唤醒。
也可以设置在方法退出时 , 断点也被唤醒。
在多态的场景下,在父类或接囗的方法上打断点,会自动调入到子类或实现类的方法。
public class Debug02 {public static void main(String[] args) {//1 在子类方法上加断点,调用后子类方法会停止Son son = new Son();son.test();//2 类的多态性,在父类方法加断点,调用子类方法仍然停止Father father = new Son();father.test();//3 接口的多态性,在接口方法上加断点,调用实现类方法仍然停止ConsumerImpl consumer = new ConsumerImpl();consumer.accept("hello");//4 对源码中加入的方法断点,调用API仍然适用HashMap map = new HashMap<>();map.put("Tom", 12);map.put("Jerry", 11);map.put("Tony", 20);}
}class Father {public void test() {System.out.println("Father : test1");System.out.println("Father : test2");}
}class Son extends Father {@Overridepublic void test() {System.out.println("Son : test1");System.out.println("Son : test2");}
}interface Consumer {void accept(String str);
}class ConsumerImpl implements Consumer {@Overridepublic void accept(String str) {System.out.println("ConsumerImpl:" + str);}
}
在类的属性声明上打断点,默认对属性的修改操作进行监控
默认情况下,在修改的时候,就会起作用。
public class Debug03 {public static void main(String[] args) {Person person = new Person(3);System.out.println(person);}
}class Person {private int id = 1;private String name;private int age;{id = 2;}public Person() {}public Person(int id) {this.id = id;}public Person(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
在代码行上加断点,右键设置条件断点的条件,当代码满足arr[i] % 3 ==0的条件下,执行断点。
public class Debug04 {public static void main(String[] args) {int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};for (int i = 0; i < arr.length; i++) {int target = arr[i];System.out.println(target);}}
}
对异常进行跟踪,如果程序出现指定异常 , 程序就会执行断点 , 自动停住 。
点击 查看断点
点击 + 添加 Java异常断点
添加 算数异常
debug调试程序
程序停留在算数异常处
public class Debug05 {public static void main(String[] args) {int m = 10;int n = 0;//算数异常int result = m / n;System.out.println(result);Person person = new Person(1001);// 空指针异常System.out.println(person.getName().toUpperCase());}
}
加行断点,右键设置线程,添加条件,线程与名字相同,就调试
public class Debug06 {public static void main(String[] args) {test("Thread1");test("Thread2");}public static void test(String threadName) {new Thread(() -> {for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}},threadName).start();}
}
右键–强制结束:不执行后面代码
public class Debug07 {public static void main(String[] args) {System.out.println("获取请求的数据");System.out.println("调用写入数据库的方法");insert();System.out.println("程序结束");}private static void insert() {System.out.println("进入insert()方法");System.out.println("获取数据库链接");System.out.println("将数据写入数据表中");System.out.println("写出操作完成");System.out.println("断开链接");}
}
在空白处右键点击
查看集合中的元素,null元素也显示
问题:使用 步入 时,会出现无法进入源码的情况,只能点击 强制进入才可以 。 如何解决 ?
方案 1:使用 强制进入 即可
方案 2:点击 - > BuiId ,Execution,DepIoyment -> Debugger -> Stepping
把 Do not step into the classess 中的 java.*、javax.* 取消勾选即可 。
关于在哪里加断点:
在可能发生错误的代码前面加断点,如果不会判断,就在程序执行的起点加断点。