【问题标题】:Breakpoint in `finally` not being hit`finally`中的断点没有被命中
【发布时间】:2018-12-20 11:52:10
【问题描述】:

我正在处理一些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 中的断点似乎没有命中,但打印成功。

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

我使用的是 Netbeans 8.1。

这是有原因的吗?

【问题讨论】:

  • 您是否处于调试模式?
  • @NiVeR 例如,我可以在try 中打断点。
  • @Shark System.out.println(); 不打印任何内容。它打印一个换行符。
  • @Shark 不,它不会在之后上线,我添加了这两行以测试两者以防万一。
  • exampleMethod()exampleMethod2() 无限相互调用。你确定这些方法都没有在任何地方被调用吗?您能否使用 exampleMethod 进行测试删除对 exampleMethod2() 的调用并查看 finally 是否仍未到达?

标签: java breakpoints


【解决方案1】:

正在发生,因为在 catch 中,如果你看到你的代码,你会无限循环 throw 函数

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() {

}

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

【讨论】:

  • 如果你看到了帖子的结尾——同样的问题在没有调用示例方法的情况下出现。
  • OP 知道这一点。 finally 仍将被执行(在StackOverflowError 被抛出之后)。
  • @achAmháin 该方法可以从其他地方调用吗?
  • @achAmháin 也许你的编辑器有问题,因为我试过了,但它碰到了刹车点
  • 当我很快回到我的机器上时,我会尝试使用其他 IDE。
【解决方案2】:

根据this answer finally 被跳过,因为无限递归(这个问题也可能与那个重复)

如果不是,我认为您应该重新编译整个项目。所以即使你删除了那个函数调用,也许编译后的类仍然有那个无限递归。

编辑

这是它有效的证明。所以请尝试重新编译它,删除缓存等,或者请给我们更多关于确切问题的描述。

【讨论】:

  • 正如我在另一个答案下面评论的那样,我没有例外地尝试过,并且结果相同。
  • 没有无限循环。有“无限递归”,但这实际上是非常有限的(由于堆栈溢出)
  • 编辑了我的答案,是的@Hulk,你是对的,这是一个无限递归,而不是循环,谢谢你指出:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 2012-03-24
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2014-12-12
相关资源
最近更新 更多