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

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

posted @ 2020-11-12 22:25  雨中遐想  阅读(1232)  评论(0编辑  收藏  举报