Java異常解析

2021-09-05 11:31:23 字數 969 閱讀 8792

2. 異常處理的基本語法

trycatch(異常型別..)catch(異常型別 ..)finally
乙個demo:

public static void main(string args)  catch (arithmeticexception e)  finally 

}

3. 自定義乙個異常
public class jsoncastexception extends runtimeexception 

}

finally用法詳解(為了能夠在異常檢測後(無論是否丟擲異常)繼續執行**,可以使用finally實現此功能)
public class demo  catch (exception e)  finally 

}public static void main(string args)

}

總結1:在finally塊中,無論**是否檢測到異常都會執行這部分的**。

在return中使用finally(不管是否有return,finally中的**也會在return之前繼續執行)
public class demo  catch (exception e)  finally 

}public static void main(string args)

}結果:

操作...

繼續執行

需要注意的是如果是system.exit(0)等情況導致jvm停止執行,finally不會執行下去。或者當乙個執行緒在執行try-finally的語句過程中被殺死,finally塊也不會執行。

另外對於finally中的返回值jvm會儲存在本地,不會和try塊中的值互動。

Java中的異常處理 基礎解析二

子類可以不丟擲異常 子類也可以丟擲異常 class father class sonextends father 子類不能丟擲異常 class father class sonextends father 只能選擇try.catch處理 不能丟擲 class father class sonexte...

XML 解析異常

parserutils warning org.xml.sax.saxparseexception uri was not reported to parser for entity document parserutils warning org.xml.sax.saxparseexception...

python異常解析

attributeerror 試圖訪問乙個物件沒有的樹形,比如foo.x,但是foo沒有屬性x ioerror 輸入 輸出異常 基本上是無法開啟檔案 importerror 無法引入模組或包 基本上是路徑問題或名稱錯誤 indentationerror 語法錯誤 的子類 沒有正確對齊 indexer...