eclipse的断点调试的几种方法

来源:互联网 发布:淘宝卖家二手开店流程 编辑:程序博客网 时间:2024/06/04 18:44

eclipse的断点调试的几种方法

linebreakpoint

我们可以在断点处设置某种条件,当达到该条件的时候才会停在断点处:

    private int x;    public void setX(int x) {        this.x = x;//断点处    }

在断点处设置属性:
这里写图片描述
只有在x是偶数的时候,才会进入断点处。

watchpoint

watchpoint描述(http://en.wikipedia.org/wiki/Breakpoint):
Other kinds of conditions can also be used, such as the reading, writing, or modification of a specific location in an area of memory. This is often referred to as a conditional breakpoint, a data breakpoint, or a watchpoint.
当设置了一个watchpoint,就告诉IDE你希望监视一块内存。当被监视的内存的内容发生变化时(reading,writing,or modification),watchpoint就被触发了,此时程序暂停运行,等待我们查看,由于我们监控的是一块内存区域那么我们实际上是监控所在的内存空间

public class BreakPoint {    private int value = 0;//这里有一个watchpoint    private Random random = new Random();    public void setValue(int count) {        for (int i = 0; i < count; i++) {            value = random.nextInt(10);            System.out.println(value);        }    }}

默认情况下watchpoint的property在读写的时候均进入变量查看:
这里写图片描述
也就是说以下两行代码在运行的时候均会停下

value = random.nextInt(10);System.out.println(value);

取消access(访问)的特性,那么下面代码就会跳过(访问value值)

System.out.println(value);

同理,取消modify(修改)的特性,那么下面代码会跳过监视(修改value值)

value = random.nextInt(10);

ExceptionBreakPoint

这里写图片描述
添加某个Exception(如IllegalArgumentException),那么在当程序执行到异常代码的时候会触发断点。例子:

    public void setValue(int count) {        for (int i = 0; i < count; i++) {            value = 4;        }    }    void printValue(int count) {        setValue(count);        if (value % 4 == 0) {            throw new IllegalArgumentException("这个value值是不合法的");        }        System.out.println(value);    }

当程序运行到异常的时候,会触发断点。我们可以跟踪异常附近的变量值。

MethodBreakPoint

修改方法断点的属性,勾选Exit的选项
这里写图片描述

    public void setValue(int count) {//该行设个断点        for (int i = 0; i < count; i++) {            value = random.nextInt(10);        }    }

那么在进入方法和离开方法的手都会暂停。


注意debug的环境配置要和build的环境配置一致,否者如果build的版本较高:
这里写图片描述
debug的版本较低:
这里写图片描述
会出现如下的类版本错误的信息:

Exception in thread "main" java.lang.UnsupportedClassVersionError: test/ConcurrentHashMapTest : Unsupported major.minor version 52.0
0 0
原创粉丝点击