Java 在循环里发生异常会跳出循环


不知道他验证了没。。。

package com.zjw;

/**
 * @author 朱俊伟
 * @date 2020/11/12 22:09
 */
public class TestError {
    public static void main(String[] args) {
        testWhileException();
        //testForException();
        System.out.println("我是main结束。。。");
    }

    public static void testWhileException(){
        int a = 1;
        int b = 2;
        while (b>a){
            System.out.println("我是while循环。。");
            int i = 1/0;
        }
    }
    public static void testForException(){
        for (;;){
            System.out.println("我是for循环。。。");
            int i = 1/0;
        }
    }
}

根据结果发现:在循环里发生异常,如果没有捕获的话,异常会一直向上抛,跳出循环,下面的代码也不会再执行。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM