成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

try-catch-finally中的return

fizz / 1207人閱讀

摘要:基礎(chǔ)系列的與方法類初始化順序線程池如何彈性伸縮的幾個(gè)要點(diǎn)的緩存什么場(chǎng)景下使用阻塞隊(duì)列的使用及模式中的序本文主要簡(jiǎn)述中有的情況。參考關(guān)于中的執(zhí)行順序

Java基礎(chǔ)系列

Java的hashcode與equals方法

Java類初始化順序

ThreadPoolExecutor線程池如何彈性伸縮

HashMap的幾個(gè)要點(diǎn)

Integer的緩存

什么場(chǎng)景下使用阻塞隊(duì)列

volatile的使用及DCL模式

try-catch-finally中的return

本文主要簡(jiǎn)述try-catch-finally中有return的情況。筆試面試經(jīng)??嫉健?/p> 題目

/**
 * http://qing0991.blog.51cto.com/1640542/1387200
 * Created by codecraft on 2016-03-08.
 */
public class ReturnDemo {

    /**
     * try有return,finally也有return,以finally的為準(zhǔn)
     * @return
     */
    public static int noException(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i--; //9
            return --i; //8
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //8
            return ++i; //9 返回這個(gè)
        }
    }

    public static int tryCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            return --i; //9
        }
        catch(Exception e){
            --i;
            System.out.println("i in catch - form try block is:"+i);
            return --i;
        }
        finally{
            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    /**
     * finally的return的優(yōu)先級(jí)最高
     * 但try/catch的return也會(huì)執(zhí)行
     * @return
     */
    public static int tryCatchFinallyReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i = i/0;
            return --i;
        }
        catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            --i; //9
            System.out.println("i in catch block is:"+i); //9
            return --i; //8
        }
        finally{
            System.out.println("i in finally - from try or catch block is--"+i); //8
            --i; //7
            System.out.println("i in finally block is--"+i); //7
            return --i; //6
        }
    }

    public static int tryExCatchReturn(){
        int i=10;
        try{
            System.out.println("i in try block is:"+i); //10
            i=i/0;
            return --i;
        }catch(Exception e){
            System.out.println("i in catch - form try block is:"+i); //10
            return --i; //9 返回這個(gè)
        }finally{

            System.out.println("i in finally - from try or catch block is:"+i); //9
            --i; //8
            System.out.println("i in finally block is:"+i); //8
        }
    }

    public static int tryExCatchExReturn() {
        int i = 10;
        try {
            System.out.println("i in try block is:" + i); //10
            i = i / 0;
            return --i;
        } catch (Exception e) {
            System.out.println("i in catch - form try block is:" + i); //10
            int j = i / 0; 
            return --i;
        } finally {

            System.out.println("i in finally - from try or catch block is:" + i); //10
            --i; //9
            --i; //8
            System.out.println("i in finally block is:" + i); //8
            return --i; //7 返回這個(gè)
        }
    }


    public static void main(String[] args){
        System.out.println("###" + noException()); //9
        System.out.println("###" + tryCatchReturn()); //7 
        System.out.println("###" + tryCatchFinallyReturn()); //6
        System.out.println("###" + tryExCatchReturn()); //9 
        System.out.println("###" + tryExCatchExReturn()); //7
    }
}
小結(jié)

如果finally中有return,則最后的返回以它為準(zhǔn),同時(shí)try或catch的return如果可以運(yùn)行到,也會(huì)執(zhí)行其表達(dá)式。

參考

關(guān)于Java中try-catch-finally-return的執(zhí)行順序

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/65604.html

相關(guān)文章

  • try-catch-finally,被你忽略掉的執(zhí)行順序

    摘要:是捕捉異常的神器,不管是調(diào)試還是防止軟件崩潰,都離不開(kāi)它。今天筆者介紹一下加上后的執(zhí)行順序嗯按順序執(zhí)行了。現(xiàn)在筆者在語(yǔ)句塊中故意報(bào)錯(cuò)看來(lái),和的都需要先經(jīng)過(guò)。 try-catch是捕捉異常的神器,不管是調(diào)試還是防止軟件崩潰,都離不開(kāi)它。今天筆者介紹一下加上finally后的執(zhí)行順序 function test() { try { console.log(1); } fin...

    bbbbbb 評(píng)論0 收藏0
  • try-catch-finally,被你忽略掉的執(zhí)行順序

    摘要:是捕捉異常的神器,不管是調(diào)試還是防止軟件崩潰,都離不開(kāi)它。今天筆者介紹一下加上后的執(zhí)行順序嗯按順序執(zhí)行了。現(xiàn)在筆者在語(yǔ)句塊中故意報(bào)錯(cuò)看來(lái),和的都需要先經(jīng)過(guò)。 try-catch是捕捉異常的神器,不管是調(diào)試還是防止軟件崩潰,都離不開(kāi)它。今天筆者介紹一下加上finally后的執(zhí)行順序 function test() { try { console.log(1); } fin...

    浠ラ箍 評(píng)論0 收藏0
  • “崩潰了?不可能,我全 Catch 住了” | Java 異常處理

    摘要:允許存在多個(gè),用于針對(duì)不同的異常做不同的處理。表示程序可能需要捕獲并且處理的異常。因此,我們應(yīng)該盡可能的避免通過(guò)異常來(lái)處理正常的邏輯檢查,這樣可以確保不會(huì)因?yàn)榘l(fā)生異常而導(dǎo)致性能問(wèn)題。異常表中的每一條記錄,都代表了一個(gè)異常處理器。 showImg(https://segmentfault.com/img/remote/1460000017918154?w=640&h=100); show...

    stdying 評(píng)論0 收藏0
  • 阿里小哥帶你玩轉(zhuǎn)JVM:揭秘try-catch-finally在JVM底層都干了些啥?

    摘要:當(dāng)觸發(fā)異常的字節(jié)碼的索引值在某個(gè)異常表?xiàng)l目的監(jiān)控范圍內(nèi),虛擬機(jī)會(huì)判斷所拋出的異常和該條目想要捕獲的異常是否匹配。 作者:李瑞杰目前就職于阿里巴巴,狂熱JVM愛(ài)好者讓我們準(zhǔn)備一個(gè)函數(shù):showImg(https://user-gold-cdn.xitu.io/2019/5/19/16acbce35adfefb7);然后,反編譯他的字節(jié)碼:showImg(https://user-gold-cd...

    番茄西紅柿 評(píng)論0 收藏0
  • 阿里小哥帶你玩轉(zhuǎn)JVM:揭秘try-catch-finally在JVM底層都干了些啥?

    摘要:當(dāng)觸發(fā)異常的字節(jié)碼的索引值在某個(gè)異常表?xiàng)l目的監(jiān)控范圍內(nèi),虛擬機(jī)會(huì)判斷所拋出的異常和該條目想要捕獲的異常是否匹配。 作者:李瑞杰目前就職于阿里巴巴,狂熱JVM愛(ài)好者讓我們準(zhǔn)備一個(gè)函數(shù):showImg(https://user-gold-cdn.xitu.io/2019/5/19/16acbce35adfefb7);然后,反編譯他的字節(jié)碼:showImg(https://user-gold-cd...

    番茄西紅柿 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<