国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java基礎之finally語(yǔ)句與return語(yǔ)句詳解

發(fā)布時(shí)間:2021-07-06 11:12 來(lái)源:腳本之家 閱讀:0 作者:星光_依舊燦爛 欄目: 開(kāi)發(fā)技術(shù)

目錄

一、return語(yǔ)句執行順序

finally語(yǔ)句是在return語(yǔ)句執行之后,return語(yǔ)句返回之前執行的

package exception;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代碼塊");
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代碼塊");
        }finally {
            System.out.println("finally中的代碼塊");
            if(a > 10){
                System.out.println("a > 10,"+"a="+a);
            }
        }
        return a += 50;
    }
}

運行結果:

try中的代碼塊
finally中的代碼塊
a > 10,a=20
20

注意:

a > 10,a=20的結果說(shuō)明了return a += 10已經(jīng)執行了,但是沒(méi)有直接返回,而是先去執行finally語(yǔ)句的內容,然后再去返回結果

二、覆蓋問(wèn)題

finally塊中的return語(yǔ)句會(huì )覆蓋try塊的return返回

package exception;

public class Demo07 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代碼塊");
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代碼塊");
        }finally {
            System.out.println("finally中的代碼塊");
            if(a > 10){
                System.out.println("a>10,"+"a="+a);
            }
            return 100;
        }
    }
}

運行結果:

try中的代碼塊
finally中的代碼塊
a>10,a=20
100

注意:

(1)如果try中有return語(yǔ)句,finally中也有return語(yǔ)句,最終執行的是finally中的return語(yǔ)句
(2)如果finally代碼塊中寫(xiě)了return語(yǔ)句,那么finally之后的return語(yǔ)句就變成不可到達的語(yǔ)句,需要注釋掉,否則編譯不過(guò)

如果finally語(yǔ)句沒(méi)有return語(yǔ)句覆蓋返回值,那么原來(lái)的返回值可能因為finally里的修改而改變也有可能不變

(1)測試1

package exception;

public class Demo08 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代碼塊");
            return a += 20;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("catch中的代碼塊");
        }finally {
            System.out.println("finally中的代碼塊");
            a  += 20;
            if(a > 10){
                System.out.println("a > 10,a="+a);
            }
            a += 20;
        }
        return 200;
    }
}

運行結果:

try中的代碼塊
finally中的代碼塊
a > 10,a=50
30

注意:

對于基本數據類(lèi)型來(lái)說(shuō),finally中對返回值的修改不會(huì )影響try中的返回變量的值

(2)測試2

package exception;

import java.util.HashMap;
import java.util.Map;

public class Demo09 {
    public static void main(String[] args) {
        System.out.println(getMap().get("KEY").toString());
    }

    public static Map<String,String> getMap(){
        Map<String,String> map = new HashMap<>();
        map.put("KEY","INIT");
        try{
            map.put("KEY","try");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("KEY","catch");
        }finally {
            map.put("KEY","finally");
            map = null;
        }
        return map;
    }
}

運行結果:

finally

注意:

對于引用數據類(lèi)型來(lái)說(shuō),finally中對返回值的修改會(huì )影響try中的返回變量的值

三、異常情況

try塊中的return語(yǔ)句在異常的情況下不會(huì )被執行

package exception;

public class Demo10 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代碼塊");
            a = a/0;
            return a += 50;
        }catch (Exception e){
            a += 15;
            System.out.println("catch中的代碼塊");
        }finally {
            System.out.println("finally中的代碼塊");
            if(a > 20){
                System.out.println("a > 20,a ="+a);
            }
            a += 10;
        }
        return a;
    }
}

運行結果:

try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 20,a =25
35

注意:

try語(yǔ)句塊中發(fā)生異常,try語(yǔ)句異常后的內容不會(huì )執行,return語(yǔ)句也不會(huì )執行,執行的是捕獲到的catch語(yǔ)句塊和finally語(yǔ)句塊

try中發(fā)生異常時(shí),return寫(xiě)在catch語(yǔ)句中

package exception;

public class Demo11 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代碼塊");
            a = a /0;
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代碼塊");
            return a += 15;
        }finally {
            System.out.println("finally中的代碼塊");
            if (a > 10){
                System.out.println("a > 10, a = "+a);
            }
            a += 50;
            System.out.println(a);
        }
    }
}

運行結果:

try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 10, a = 25
75
25

注意:

try中發(fā)生異常之后,catch中的return語(yǔ)句先執行,確定了返回值之后(保存起來(lái),finally中的語(yǔ)句對返回值無(wú)影響)再去finally語(yǔ)句塊,執行完之后再返回a的值,finally中對a的修改對返回值無(wú)效

四、finally語(yǔ)句一定會(huì )被執行嗎?

(1)當程序進(jìn)入try語(yǔ)句之前就出現異常時(shí),會(huì )直接結束

(2)try語(yǔ)句塊中有強制退出時(shí)也不會(huì )執行finally語(yǔ)句塊中的代碼

System.exit(0);

代碼示例:

package exception;

public class Demo12 {
    public static void main(String[] args) {
        int a = 10;
        try{
            System.out.println("try block");
            System.exit(0);
        }catch (Exception e){
            System.out.println("catch block");
        }finally {
            System.out.println("finally block");
        }
    }
}

運行結果:

try block

到此這篇關(guān)于Java基礎之finally語(yǔ)句與return語(yǔ)句詳解的文章就介紹到這了,更多相關(guān)java finally語(yǔ)句與return語(yǔ)句內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

色88久久久久高潮综合影院| 国产在线不卡一区二区三区| 国产日产欧洲无码视频无遮挡| 狠狠五月激情六月丁香| 国产成人精品久久亚洲高清不卡| 久久九九久精品国产综合一千收藏|