欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況詳析

 更新時(shí)間:2024年05月21日 09:04:57   作者:Mr_X_X  
當(dāng)產(chǎn)生異常后,并在異常處理器中進(jìn)行執(zhí)行之后,程序會(huì)是如何的一種狀態(tài),是終止還是繼續(xù)執(zhí)行處理之后的代碼呢,下面這篇文章主要給大家介紹了關(guān)于JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況的相關(guān)資料,需要的朋友可以參考下

一、出現(xiàn)異常時(shí),try-catch對(duì)代碼執(zhí)行的影響

1.1、不加try-catch

//情形1:不加try-catch,出現(xiàn)異常,后續(xù)代碼不再執(zhí)行
    @Test
    public void test1() {
        String a = null;
        int c = 0;
        int b = a.length();  //null沒有l(wèi)ength()方法,報(bào)空指針異常錯(cuò)誤
        //下面兩條賦值語句不會(huì)執(zhí)行
        System.out.println("c的值為:" + c);
    }

結(jié)果如下: 

結(jié)論: 

1、System.out.println("c的值為:" + c);不執(zhí)行

2、不加try-catch,出現(xiàn)異常,后續(xù)代碼不再執(zhí)行

1.2、加上try-catch

//情形2:加上try-catch,出現(xiàn)異常,try中出現(xiàn)異常的那一行代碼的后續(xù)代碼不再執(zhí)行
    //catch中的代碼正常執(zhí)行
    @Test
    public void test2() {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null沒有l(wèi)ength()方法,報(bào)空指針異常錯(cuò)誤
            //下面兩條賦值語句不會(huì)執(zhí)行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值為:" + c);
        }
        System.out.println("d的值為:" + d);   //本條語句會(huì)執(zhí)行

    }

結(jié)果如下:

結(jié)論: 

1、加上try-catch,出現(xiàn)異常時(shí),try中出現(xiàn)異常的那一行代碼的后續(xù)代碼不再執(zhí)行

2、catch中的代碼正常執(zhí)行,并且catch之后的代碼也會(huì)執(zhí)行

1.3、 加上try-catch,并在try中拋出現(xiàn)異常

//情形3:加上try-catch,并在try中拋出現(xiàn)異常,
    // try中拋出異常的那一行代碼的后續(xù)代碼不再執(zhí)行
    //catch中的代碼正常執(zhí)行
    @Test
    public void test3() {
        String a = null;
        int c = 0, d = 0;
        try {
            if (a == null) {
                throw new RuntimeException("a的值不能是空");
            }
            //下面兩條賦值語句不會(huì)執(zhí)行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值為:" + c); //會(huì)執(zhí)行
        }
        System.out.println("d的值為:" + d); //會(huì)執(zhí)行

    }

結(jié)果如下:

結(jié)論:

1、加上try-catch,并在try中拋出現(xiàn)異常,try中拋出異常的那一行代碼的后續(xù)代碼不再執(zhí)行

2、catch中的代碼正常執(zhí)行,并且catch之后的代碼也會(huì)執(zhí)行

1.4、加上try-catch,并在catch中拋出現(xiàn)異常

//情形4:加上try-catch,并在catch中拋出現(xiàn)異常,
    //try中出現(xiàn)異常的那一行代碼的后續(xù)代碼不再執(zhí)行
    //catch中的代碼只執(zhí)行throw之前的代碼
    @Test
    public void test4() {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null沒有l(wèi)ength()方法,報(bào)空指針異常錯(cuò)誤
            //下面兩條賦值語句不會(huì)執(zhí)行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值為:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值為:" + d);   //本條語句也不執(zhí)行

    }

結(jié)果如下:

結(jié)論:

 1、加上try-catch,并在catch中拋出現(xiàn)異常,try中出現(xiàn)異常的那一行代碼的后續(xù)代碼不再執(zhí)行。

2、catch中的代碼只執(zhí)行throw之前的代碼

二、循環(huán)時(shí)出現(xiàn)異常后續(xù)代碼是否執(zhí)行

我們需要明確的一點(diǎn)是,當(dāng)出現(xiàn)異常時(shí),Java中的for循環(huán)默認(rèn)會(huì)停止執(zhí)行,不會(huì)進(jìn)行下一次循環(huán)。

 2.1、循環(huán)中不加try-catch

//不加try-catch,當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)
    @Test
    public void test5() {
        String title = "我要拿高薪";
        for (int i = 0; i < title.length(); i++) {
            System.out.println("執(zhí)行了第" + i + "次");
            int j = 1 / 0;

        }
    }

結(jié)果如下:

 結(jié)論:

不加try-catch,當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)

2.2、循環(huán)中加try-catch 

//加try-catch,當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)
    @Test
    public void test6() {
        String title = "我要拿高薪";
        try {
            for (int i = 0; i < title.length(); i++) {
                System.out.println("執(zhí)行了第" + i + "次");
                int j = 1 / 0;

            }
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
        }

    }

 結(jié)果如下:

 結(jié)論:

加try-catch,當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)

2.3、加try-catch,并在catch中將異常拋出

//加try-catch,并在catch中將異常拋出
    //當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)
    @Test
    public void test7() {
        String title = "我要拿高薪";
        try {
            for (int i = 0; i < title.length(); i++) {
                System.out.println("執(zhí)行了第" + i + "次");
                int j = 1 / 0;

            }
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
            throw e;
        }

    }

 結(jié)果如下:

結(jié)論:

加try-catch,并在catch中將異常拋出,當(dāng)循環(huán)中出現(xiàn)異常時(shí),循環(huán)會(huì)終止,不會(huì)繼續(xù)下一次循環(huán)

2.4、在for循環(huán)中加上嵌套try-catch并使用continue

@Test
    public void test8() {
        String title = "我要拿高薪";
        try {
            for (int i = 0; i < title.length(); i++) {
                try {
                    System.out.println("執(zhí)行了第" + i + "次");
                    int j = 1 / 0;
                } catch (Exception e) {
                    System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
                    continue;
                }


            }
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
        }

    }

  結(jié)果如下:

 結(jié)論:

在for循環(huán)中加上嵌套try-catch,并在catch中使用continue,可以使循環(huán)一直執(zhí)行下去。

三、調(diào)用的方法內(nèi)部出現(xiàn)異常

3.1、調(diào)用的方法內(nèi)部出現(xiàn)異常

 @Test
    public void test9() {
        try {
            save1();
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
        }

    }

    public void save1() {
        System.out.println("我要當(dāng)高級(jí)JAVA開發(fā)工程師");
        int i = 1 / 0;
        
    }

  結(jié)果如下:

  結(jié)論:

方法內(nèi)部不對(duì)異常做任何處理,調(diào)用處的catch可以捕獲到異常。

3.2、調(diào)用的方法內(nèi)部出現(xiàn)異常并捕獲

@Test
    public void test9() {
        try {
            save2();
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
        }
    }

    public void save2() {
        try {
            System.out.println("我要當(dāng)高級(jí)JAVA開發(fā)工程師");
            int i = 1 / 0;
        } catch (Exception e) {
            System.out.println("方法內(nèi)部出現(xiàn)了異常" + e);
        }
    }

 結(jié)果如下:

結(jié)論:

 方法內(nèi)部對(duì)異常進(jìn)行捕獲,調(diào)用處的catch不能捕獲到異常。

3.3、調(diào)用的方法內(nèi)部出現(xiàn)異常并捕獲最后拋出

@Test
    public void test9() {
        try {
            save3();
        } catch (Exception e) {
            System.out.println("循環(huán)中出現(xiàn)了異常:" + e);
        }
    }
    
    public void save3() {
        try {
            System.out.println("我要當(dāng)高級(jí)JAVA開發(fā)工程師");
            int i = 1 / 0;
        } catch (Exception e) {
            System.out.println("方法內(nèi)部出現(xiàn)了異常" + e);
            throw e;
        }
    }

結(jié)果如下:

結(jié)論:

 方法內(nèi)部對(duì)異常進(jìn)行捕獲,最后拋出。調(diào)用處的catch可以捕獲到異常。

總結(jié)

到此這篇關(guān)于JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況詳析的文章就介紹到這了,更多相關(guān)JAVA拋出異常代碼是否執(zhí)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論