即使在 StackOverflow 之后代码也能正常工作 - 怎么样?

Code working fine even after StackOverflow - How?

根据我的理解,下面的代码应该打印 0 作为输出,因为堆栈已满,它应该立即退出方法。

然而,当我 运行 以下代码时,它在第一种情况下打印 100,在第二种情况下打印 1

class ErrorAndException {
        public static int callWhosebug() {
            try {
                callWhosebug();
                return 100;
            } catch (Error e) {
                System.out.println(e);
                return 0;
            } finally {
            }
        }

        public static void main(String[] args) {
            System.out.println(callWhosebug());
        }
    }

案例 - 2

class ErrorAndException {
            public static int callWhosebug() {
                try {
                    callWhosebug();
                    return 100;
                } catch (Error e) {
                    System.out.println(e);
                    return 0;
                } finally {
                           return 1
                }
            }

        public static void main(String[] args) {
            System.out.println(callWhosebug());
        }
    }

请帮助我理解这种行为。

仅对 callWhosebug() 的最终调用(抛出 WhosebugError 的调用)returns 0。但是当它 returns 时,之前对 callWhosebug() 的调用都是 return 100。您的 main 方法仅打印由对 return 的初始调用编辑的值=11=],也就是 100.

如果您希望 0 被 return 编辑到 main 方法,callWhosebug() 必须 return 值 return 由递归调用编辑:

public static int callWhosebug() {
    try {
        return callWhosebug();
    } catch (Error e) {
        System.out.println(e);
        return 0;
    } finally {
    }
}

导致溢出的最后一个项目将 return 0 到前一个实例,但该值将丢失,因为所有较低的实例只是 return 100 到另一个,直到最后一个实例退出 returning 100.