亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java自定義異常類的實(shí)例詳解

 更新時(shí)間:2017年09月25日 14:19:09   作者:Lovnx  
這篇文章主要介紹了Java自定義異常類的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家學(xué)習(xí)理解掌握這部分內(nèi)容,需要的朋友可以參考下

Java自定義異常類的實(shí)例詳解

為什么要自己編寫異常類?假如jdk里面沒(méi)有提供的異常,我們就要自己寫。我們常用的類ArithmeticException,NullPointerException,NegativeArraySizeException,ArrayIndexoutofBoundsException,SecurityException這些類,都是繼續(xù)著RuntimeException這個(gè)父類,而這個(gè)父類還有一個(gè)父類是Exception。那么我們自己寫異常類的時(shí)候,也是繼續(xù)Exception這個(gè)類的。

實(shí)踐:

class MyException extends Exception { //繼續(xù)了Exception這個(gè)父類

private int detail;

MyException(int a) {

detail = a;}

public String toString() {

return "MyException[" + detail + "]";

}}

class ExceptionDemo {

static void compute(int a) throws MyException {

System.out.println("調(diào)用 compute(" + a + ")");

if(a > 10)

throw new MyException(a);

System.out.println("常規(guī)退出 ");

}

public static void main(String args[]) {

try {

compute(1);

compute(20);

} catch (MyException e) {

System.out.println("捕捉 " + e); //這樣就可以用自己定義的類來(lái)捕捉異常了

}}} 

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論