大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專(zhuān)注Java教育14年 全國(guó)咨詢(xún)/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 JAVA中異常與異常處理詳解 (二)

JAVA中異常與異常處理詳解 (二)

更新時(shí)間:2019-09-11 11:54:07 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2384次


動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編分享的“JAVA中異常與異常處理詳解”的內(nèi)容太長(zhǎng),請(qǐng)看上文:http://www.dabaquan.cn/javazixun/1828.html


Java自定義異常


  如果要自定義異常類(lèi),則擴(kuò)展Exception類(lèi)即可,因此這樣的自定義異常都屬于檢查異常(checked exception)。如果要自定義非檢查異常,則擴(kuò)展自RuntimeException。


  按照國(guó)際慣例,自定義的異常應(yīng)該總是包含如下的構(gòu)造函數(shù):


  一個(gè)無(wú)參構(gòu)造函數(shù)


  一個(gè)帶有String參數(shù)的構(gòu)造函數(shù),并傳遞給父類(lèi)的構(gòu)造函數(shù)。


  一個(gè)帶有String參數(shù)和Throwable參數(shù),并都傳遞給父類(lèi)構(gòu)造函數(shù)


  一個(gè)帶有Throwable 參數(shù)的構(gòu)造函數(shù),并傳遞給父類(lèi)的構(gòu)造函數(shù)。


  下面是IOException類(lèi)的完整源代碼:


public class IOException extends Exception

{

    static final long serialVersionUID = 7818375828146090155L;


    public IOException()

    {

        super();

    }


    public IOException(String message)

    {

        super(message);

    }


    public IOException(String message, Throwable cause)

    {

        super(message, cause);

    }


    

    public IOException(Throwable cause)

    {

        super(cause);

    }

}



Java異常的注意事項(xiàng)


1、當(dāng)子類(lèi)重寫(xiě)父類(lèi)的帶有 throws聲明的函數(shù)時(shí),其throws聲明的異常必須在父類(lèi)異常的可控范圍內(nèi)——用于處理父類(lèi)的throws方法的異常處理器,必須也適用于子類(lèi)的這個(gè)帶throws方法 。這是為了支持多態(tài)。


  例如,父類(lèi)方法throws 的是2個(gè)異常,子類(lèi)就不能throws 3個(gè)及以上的異常。父類(lèi)throws IOException,子類(lèi)就必須throws IOException或者IOException的子類(lèi)。


示例:


class Father

{

    public void start() throws IOException

    {

        throw new IOException();

    }

}


class Son extends Father

{

    public void start() throws Exception

    {

        throw new SQLException();

    }

}

/**********************假設(shè)上面的代碼是允許的(實(shí)質(zhì)是錯(cuò)誤的)***********************/

class Test

{

    public static void main(String[] args)

    {

        Father[] objs = new Father[2];

        objs[0] = new Father();

        objs[1] = new Son();


        for(Father obj:objs)

        {

        //因?yàn)镾on類(lèi)拋出的實(shí)質(zhì)是SQLException,而IOException無(wú)法處理它。

        //那么這里的try。。catch就不能處理Son中的異常。

        //多態(tài)就不能實(shí)現(xiàn)了。

            try {

                 obj.start();

            }catch(IOException)

            {

                 //處理IOException

            }

         }

   }

}


  2、Java程序可以是多線程的。每一個(gè)線程都是一個(gè)獨(dú)立的執(zhí)行流,獨(dú)立的函數(shù)調(diào)用棧。如果程序只有一個(gè)線程,那么沒(méi)有被任何代碼處理的異常 會(huì)導(dǎo)致程序終止。如果是多線程的,那么沒(méi)有被任何代碼處理的異常僅僅會(huì)導(dǎo)致異常所在的線程結(jié)束。


  也就是說(shuō),Java中的異常是線程獨(dú)立的,線程的問(wèn)題應(yīng)該由線程自己來(lái)解決,而不要委托到外部,也不會(huì)直接影響到其它線程的執(zhí)行。



finally塊和return


首先一個(gè)不容易理解的事實(shí):在 try塊中即便有return,break,continue等改變執(zhí)行流的語(yǔ)句,finally也會(huì)執(zhí)行。


public static void main(String[] args)

{

    int re = bar();

    System.out.println(re);

}

private static int bar() 

{

    try{

        return 5;

    } finally{

        System.out.println("finally");

    }

}

/*輸出:

finally

*/


很多人面對(duì)這個(gè)問(wèn)題時(shí),總是在歸納執(zhí)行的順序和規(guī)律,小編總結(jié)了一個(gè)方法。用如下GIF圖說(shuō)明。


1568173757953680.gif


  也就是說(shuō):try...catch...finally中的return 只要能執(zhí)行,就都執(zhí)行了,他們共同向同一個(gè)內(nèi)存地址(假設(shè)地址是0x80)寫(xiě)入返回值,后執(zhí)行的將覆蓋先執(zhí)行的數(shù)據(jù),而真正被調(diào)用者取的返回值就是最后一次寫(xiě)入的。那么,按照這個(gè)思想,下面的這個(gè)例子也就不難理解了。


  finally中的return 會(huì)覆蓋 try 或者catch中的返回值。


public static void main(String[] args)

    {

        int result;

        

        result  =  foo();

        System.out.println(result);     /////////2

        

        result = bar();

        System.out.println(result);    /////////2

    }


    @SuppressWarnings("finally")

    public static int foo()

    {

        trz{

            int a = 5 / 0;

        } catch (Exception e){

            return 1;

        } finally{

            return 2;

        }


    }


    @SuppressWarnings("finally")

    public static int bar()

    {

        try {

            return 1;

        }finally {

            return 2;

        }

    }


finally中的return會(huì)抑制(消滅)前面try或者catch塊中的異常


class TestException

{

    public static void main(String[] args)

    {

        int result;

        try{

            result = foo();

            System.out.println(result);           //輸出100

        } catch (Exception e){

            System.out.println(e.getMessage());    //沒(méi)有捕獲到異常

        }

        

        

        try{

            result  = bar();

            System.out.println(result);           //輸出100

        } catch (Exception e){

            System.out.println(e.getMessage());    //沒(méi)有捕獲到異常

        }

    }

    

    //catch中的異常被抑制

    @SuppressWarnings("finally")

    public static int foo() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }catch(ArithmeticException amExp) {

            throw new Exception("我將被忽略,因?yàn)橄旅娴膄inally中使用了return");

        }finally {

            return 100;

        }

    }

    

    //try中的異常被抑制

    @SuppressWarnings("finally")

    public static int bar() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }finally {

            return 100;

        }

    }

}


finally中的異常會(huì)覆蓋(消滅)前面try或者catch中的異常


class TestException

{

    public static void main(String[] args)

    {

        int result;

        try{

            result = foo();

        } catch (Exception e){

            System.out.println(e.getMessage());    //輸出:我是finaly中的Exception

        }

        

        

        try{

            result  = bar();

        } catch (Exception e){

            System.out.println(e.getMessage());    //輸出:我是finaly中的Exception

        }

    }

    

    //catch中的異常被抑制

    @SuppressWarnings("finally")

    public static int foo() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }catch(ArithmeticException amExp) {

            throw new Exception("我將被忽略,因?yàn)橄旅娴膄inally中拋出了新的異常");

        }finally {

            throw new Exception("我是finaly中的Exception");

        }

    }

    

    //try中的異常被抑制

    @SuppressWarnings("finally")

    public static int bar() throws Exception

    {

        try {

            int a = 5/0;

            return 1;

        }finally {

            throw new Exception("我是finaly中的Exception");

        }

        

    }

}


注意:


  不要在fianlly中使用return。


  不要在finally中拋出異常。


  減輕finally的任務(wù),不要在finally中做一些其它的事情,finally塊僅僅用來(lái)釋放資源是最合適的。


  將盡量將所有的return寫(xiě)在函數(shù)的最后面,而不是try ... catch ... finally中。


以上就是動(dòng)力節(jié)點(diǎn)Java培訓(xùn)機(jī)構(gòu)小編介紹的“JAVA中異常與異常處理詳解”的內(nèi)容,希望對(duì)大家有幫助,更多Java最新資訊請(qǐng)繼續(xù)關(guān)注動(dòng)力節(jié)點(diǎn)Java培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。


相關(guān)視頻教程推薦


java初級(jí)入門(mén)教程下載——Java自定義異常:http://www.dabaquan.cn/xiazai/1023.html


java初級(jí)入門(mén)教程下載——異常捕獲預(yù)處理:http://www.dabaquan.cn/xiazai/2555.html



提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-07-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产在线视频你懂得 | 91亚洲精品国产自在现线 | 成人日韩视频 | 欧美成人免费在线视频 | 国内精品免费久久影院 | 91视频国产91久久久 | 亚洲精品一区二区三区在线看 | 国产成人亚洲精品大帝 | 亚洲偷自拍另类图片二区 | 久色阁 | 国内精品91久久久久 | 狠狠操天天操 | 国产成人精品三级91在线影院 | 在线观看深夜观看网站免费 | 天天操操操操操操 | 一区两区三不卡 | 日本一级毛片aaaaa | 色久激情| 老司机深夜影院入口aaaa | 亚洲伊人久久综合 | 久久国产精品亚洲77777 | 日韩欧美一级大片 | julia中文字幕在线 | 欧美午夜久久 | 国产精品久久久久久久久久日本 | 狠狠干天天爽 | 综合色久七七综合七七蜜芽 | 中文视频在线 | 国产一级特黄生活片 | 天天操夜夜逼 | 四虎最新紧急入口 | 免费h片在线观看 | 色综合天天综合网国产国产人 | 国产精品爱久久 | 欧美高清亚洲欧美一区h | 一级毛片成人免费看a | 精品国产高清a毛片无毒不卡 | 伊人久久综合 | 国产成人精品亚洲77美色 | 亚洲啪视频 | 99热久久免费精品首页 |