最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java異常--常見方法--自定義異常--增強(qiáng)try(try-with-resources)詳解

 更新時間:2023年03月14日 10:38:07   作者:一只小余  
這篇文章主要介紹了Java異常--常見方法--自定義異常--增強(qiáng)try(try-with-resources)的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

異常方法

//返回此可拋出對象的詳細(xì)信息消息字符串
public String getMessage() 

//將此可拋發(fā)對象及其回溯到標(biāo)準(zhǔn)錯誤流。此方法在錯誤輸出流上打印此 Throwable 對象的堆棧跟蹤
//最為詳細(xì)
public void printStackTrace()
//返回此可拋件的簡短說明
public String toString()

對于1/0這個異常

 try{
            int i = 1/0;
        } catch(Exception e){
            System.out.println("e = " + e);
            System.out.println("-----------------");
            System.out.println("e.getMessage() = " + e.getMessage());
            System.out.println("-----------------");
            System.out.println("e.getStackTrace() = " + Arrays.toString(e.getStackTrace()));
            System.out.println("-----------------");
            System.out.println("e.getLocalizedMessage() = " + e.getLocalizedMessage());
            System.out.println("-----------------");
            System.out.println("e.getCause() = " + e.getCause());
            System.out.println("-----------------");
            System.out.println("e.getClass() = " + e.getClass());
            System.out.println("-----------------");
            System.out.println("e.getSuppressed() = " + Arrays.toString(e.getSuppressed()));

        }
e = java.lang.ArithmeticException: / by zero
-----------------
e.getMessage() = / by zero
-----------------
e.getStackTrace() = [省略27行,com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)]
-----------------
//可能的原因
e.getCause() = null
-----------------
//一個數(shù)組,其中包含為傳遞此異常而禁止顯示的所有異常。
//就是用try捕獲卻不做事的
e.getSuppressed() = []

自定義異常

作用

讓控制臺的報(bào)錯信息更加的見名知意

定義

1.定義異常類,寫繼承關(guān)系。
名字要見名知義,繼承于異常類。
像運(yùn)行時可以繼承RuntimeException
在開發(fā)過程中一般會有多種異常類,小的會繼承自定義的大的。

2.寫構(gòu)造方法
需要書寫空參和帶參的構(gòu)造。
可以調(diào)用父類的也可以自定義

增強(qiáng)try(try-with-resources)

作用

簡化釋放資源的步驟

條件

自動釋放的類需要實(shí)現(xiàn)autocloseable的接口
這樣在特定情況下會自動釋放,還有的就是stream流中提到過。

jdk7

try(創(chuàng)建對象資源1;創(chuàng)建對象資源2){

}catch(){
}

例如這樣的代碼可以改寫成

BufferedInputStream b = null;
try {
    b = new BufferedInputStream(new FileInputStream(""));
}catch (Exception e) {
    e.printStackTrace();
}finally {
    if (b!=null) {
        try {
            b.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
try (BufferedInputStream b = new BufferedInputStream(new FileInputStream(""));){
    
}catch (Exception e) {
    e.printStackTrace();
}

jdk9

創(chuàng)建對象1
創(chuàng)建對象2
try(變量名1;變量名2){
}catch(){
}

上面的代碼可以改寫成,
不過需要注意的是創(chuàng)建對象也需要異常處理,我們這里選擇拋出

public void testTryWithResource() throws FileNotFoundException {
    BufferedInputStream b = new BufferedInputStream(new FileInputStream(""));
    try (b) {

    } catch (Exception e) {
        e.printStackTrace();
    }
}

到此這篇關(guān)于java-異常--常見方法--自定義異常--增強(qiáng)try(try-with-resources)的文章就介紹到這了,更多相關(guān)java自定義異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

苍山县| 江都市| 应用必备| 大竹县| 乌鲁木齐县| 安远县| 武陟县| 荆州市| 南京市| 锡林浩特市| 莫力| 交城县| 三都| 汉中市| 佛山市| 舟山市| 伽师县| 凭祥市| 芦溪县| 宜兰市| 武鸣县| 东明县| 庆安县| 鲜城| 清丰县| 高雄市| 小金县| 抚州市| 遵化市| 长葛市| 丽江市| 三明市| 礼泉县| 牡丹江市| 新河县| 驻马店市| 定西市| 土默特右旗| 富民县| 石楼县| 大洼县|