try catch 放在 for循环外面和里面区别

文章比较了trycatch在for循环外和内的使用效果,指出外部异常会导致循环中断,而内部则能捕获异常继续执行。同时,分析了trycatch在for循环内部的内存消耗情况,对大量异常处理时可能产生的影响。
摘要由CSDN通过智能技术生成

1. try catch 在for循环外面

public class TryCatchDemo1 {
    public static void main(String[] args) {
        tryOutside();
    }

    public static void tryOutside() {
        try {
            for (int count = 1; count <= 5; count++) {
                if (count == 3) {
                    //故意制造一下异常
                    int num = 1 / 0;
                } else {
                    System.out.println("count:" + count + " 业务正常执行");
                }
            }
        } catch (Exception e) {
            System.out.println("try catch  在for 外面的情形, 出现了异常,for循环显然被中断");
        }
    }
}

执行结果:

count:1 业务正常执行
count:2 业务正常执行
try catch  在for 外面的情形, 出现了异常,for循环显然被中断

结论:

try  catch  在 for 循环 外面 的时候, 如果 for循环过程中出现了异常, 那么for循环会终止。

2. try catch 在for循环里面

public class TryCatchDemo2 {
    public static void main(String[] args) {
        tryInside();
    }

    public static void tryInside() {
        for (int count = 1; count <= 5; count++) {
            try {
                if (count == 3) {
                    //故意制造一下异常
                    int num = 1 / 0;
                } else {
                    System.out.println("count:" + count + " 业务正常执行");
                }
            } catch (Exception e) {
                System.out.println("try catch  在for 里面的情形, 出现了异常,for循环显然继续执行");
            }
        }
    }
}

执行结果:

count:1 业务正常执行
count:2 业务正常执行
try catch  在for 里面的情形, 出现了异常,for循环显然继续执行
count:4 业务正常执行
count:5 业务正常执行

结论:

try  catch  在 for 循环 里面 的时候, 如果 for循环过程中出现了异常,异常被catch抓掉,不影响for循环 继续执行。

3. 性能差别

统计一下try catch 在for循环里面的内存消耗情况

public class TryCatchDemo3 {
    public static void main(String[] args) {
        tryInsideCensus();
    }

    public static void tryInsideCensus() {
        long beginTime = System.currentTimeMillis();
        Runtime runtime = Runtime.getRuntime();
        // 获取当前剩余内存
        long memory = runtime.freeMemory();
        System.out.println("memory: " + memory);

        for (int count = 1; count <= 300000; count++) {
            try {
                if (count == 2) {
                    //故意制造一下异常
                    int num = 1 / 0;
                } else {
                    System.out.println("count:" + count + " 业务正常执行");
                }
            } catch (Exception e) {
                System.out.println("try catch  在for 里面的情形");
            }
        }

        long endTime = System.currentTimeMillis();
        System.out.println("endTime - beginTime = " + (endTime - beginTime));
        System.out.println("freeMemory1: " + memory);
        System.out.println("freeMemory2: " + runtime.freeMemory());
        System.out.println("freeMemory1 - freeMemory2 = " + (memory - runtime.freeMemory())/10000 + "W bytes");
    }
}

执行结果:

endTime - beginTime = 2602
freeMemory1: 109171744
freeMemory2: 104393928
freeMemory1 - freeMemory2 = 477W bytes

结论:

也就是说, try catch 放在 for 循环里面 ,因为出现异常不会终止 for循环。所以如果真的存在大批量业务处理全是异常,有那么一定的内存消耗情况。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值