首页 > 解决方案 > `finally`中的断点没有被命中

问题描述

我正在搞乱一些try...catch...finally处决,并注意到finally似乎不会命中断点:

    try {
        System.out.println("in try");
        int num = 5 / 0;
    } catch (ArithmeticException e) {
        System.out.println("in catch");
    } finally {
        System.out.println(); //breakpoint here
        System.out.println("in finally");
    }

中的断点finally似乎没有命中,但它打印成功。

但是,如果我将 更改tryint num = 5 / 1;,因此不进入catch,则断点命中。

我正在使用 Netbeans 8.1。

是否有一个原因?

标签: javabreakpoints

解决方案


正在发生,因为在 catch 中,如果您看到代码,您将无限循环抛出函数

exampleMethod() 正在调用 exampleMethod2() 和 exampleMethod2() 调用 exampleMethod() 所以你有一个带有函数的循环,这就是你得到 StackoverFlowError 的原因

尝试不调用自身的函数

    public static void main(String[] args) {
    SpringApplication.run(AuthApplication.class, args);
    try {
        System.out.println("in try");
        int num = 5 / 0;
    } catch (ArithmeticException e) {
        System.out.println("in catch");
        exampleMethod();
    } finally {
        System.out.println(); // <--- breakpoint here
        System.out.println("in finally");
    }
}

static void exampleMethod() {

}

在这个例子中,刹车点终于命中了


推荐阅读