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