1

I was messing around with some try...catch...finally executions and noticed a breakpoint in finally won't seem to be hit:

    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");
    }

The breakpoint in the finally doesn't seem to hit, however it prints out successfully.

However, if I change the try to int num = 5 / 1;, and therefore not going in to the catch, the breakpoint is hit.

I'm using Netbeans 8.1.

Is there a reason for this?

achAmháin
  • 4,176
  • 4
  • 17
  • 40

2 Answers2

0

is happening because in catch you are infinitely looping throw functions if you see your code

exampleMethod() is calling exampleMethod2() and exampleMethod2() calling exampleMethod() so you have a loop with function thats why you get StackoverFlowError

try to functions not call itself

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

}

in this example brakepoint hit in finally

Ricard Kollcaku
  • 1,622
  • 5
  • 9
0

According to this answer finally is skipped because the infinite recursion (also this question might be a duplicate of that)

Or if not i think you should recompile the whole project. So even if you delete that function call, maybe the compiled classes still have that infinite recursion.

EDIT

Here is the proof that it works. So please try to recompile it, delete caches, etc, or please give us a bit more description about the exact problem.

enter image description here

aBnormaLz
  • 809
  • 6
  • 22
  • 1
    As I commented below the other answer, I tried without the exception and the same result applies. – achAmháin Dec 20 '18 at 12:10
  • There is no infinite loop. There is "infinfite recursion", but this is actually quite finite (due to stack overflow) – Hulk Dec 20 '18 at 12:12
  • Edited my answer, and yes @Hulk you are right, that is an infinite recursion, not loop, thanks for pointing that out :) – aBnormaLz Dec 20 '18 at 12:13