heng_zou 2022-10-16 09:16 采纳率: 50%
浏览 111
已结题

Java try catch finally无限递归问题

执行如下代码环,其递归循环次数将达到3^N次(N为最大栈深)为天文数字因此几乎无限循环

    public static void foo() {
        try {
            System.out.println("try");
            foo();
        } catch (Throwable e) {
            System.out.println("catch");
            foo();
        } finally {
            System.out.println("finally");
            foo();
        }
    }

上面代码控制台打印内容如下,我好奇的为什么没有输出“catch”,将上述的catch (Throwable e) 换成catch (StackOverflowError e)后正常输出“catch”

···
try
try
try
try
try
try
fifinallyfinallyfinally
tryfinallyfinally
try
tryfinallyfinally
tryfinallyfinally
try
try
···

我尝试去统计try/catch/finally块的执行次数,编写如下代码,创建一个线程每隔3秒钟输出统计结果

    static Counter counter = new Counter();
    public static void main(String[] args) {
        new Thread(() -> {
            try {
                for (; ; ) {
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(counter);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        foo();
    }

    static void foo() {
        try {
            counter.t++;
            System.out.println("try");
            foo();
        } catch (Throwable e) {
            counter.c++;
            System.out.println("catch");
            foo();
        } finally {
            counter.f++;
            System.out.println("finally");
            foo();
        }
    }
    static class Counter {
        int t, c, f;

        @Override
        public String toString() {
            return "Counter{" +
                    "t=" + t +
                    ", c=" + c +
                    ", f=" + f +
                    '}';
        }
    }

结果控制台输出如下结果,可以看到try、catch、finally块的执行次数基本相同(try块初始化递归时多了一个最大栈深的次数)

try
try
trycatcatchfinallycatchfinallycatchfinally
trycatchfinally
trycatchfinallycatch
try
···
···
trycatchfinallyCounter{t=17028, c=10621, f=10615}

可是当我将创建输出统计结果的线程的代码注释掉后“catch”又不输出了,甚至当我仅仅创建该线程不去start()时却可以输出“catch”,有谁知道这个“catch”什么情况下输出吗?

  • 写回答

3条回答 默认 最新

  • 不懂人情世故 2022-10-16 09:42
    关注

    你这里好像处了异常也不会进入finally吧, 因为打印''catch''后又进入递归了吧,
    然后也不知道你想要干什么, 纯粹闲的慌

        public static void foo() {
            try {
                System.out.println("try");
                foo();
            } catch (Throwable e) {
                System.out.println("catch");
                foo();
            } finally {
                System.out.println("finally");
                foo();
            }
        }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月23日
  • 修改了问题 10月17日
  • 创建了问题 10月16日

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)