日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java学习笔记(七)----异常

發布時間:2025/7/14 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java学习笔记(七)----异常 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

異常
class Test
{ public int devide(int x,int y) throws Exception //throws EXception 拋出異常,如果感覺到這個程序可能出現異常,就加上這條語句,在后面的調用中,如果不加try,catch編譯就會出錯。
?? {? int result=x/y;
????? return result;
?? }
}
class TestException
{ public static void main(String[] args)
? { try
??? { new Test().devide(3,0);}
??? catch(Exception ex)
??? { System.out.println(ex.getMessage()); } //getMessage 返回異常的信息,是一個字符串,須要我們自己將這個字符串打印在屏幕上
??? System.out.println("program is running here!");
? }
}

***創建自己的異常***
class Test
{ public int devide(int x,int y) throws Exception?
?? {? if (y<0)
????????? throw new DevideByMinusException("devisor is"+y); //創建一個異常對象,并且拋出這個異常(注意這個用的是throw而不是throws.)
????? int result=x/y;
????? return result;
?? }
}
class DevideByMinusException extends Exception //創建自己的異常(除數為0時產生的異常)
{ public DevideByMinusException (String msg)
? { super(msg); }
}
class TestException
{ public static void main(String[] args)
? { try
??? { new Test().devide(3,-1);}
??? catch(Exception ex)??? //把上面生成的異常對象傳給ex
??? { System.out.println(ex.getMessage()); }???? //輸出結果為devisor is -1
??? System.out.println("program is running here!"); //這個語句,運行時一直都輸出
? }
}

***拋出多個異常***

class Test
{ public int devide(int x,int y) throws ArithmeticException,DevideByMinusException?
?? {? if (y<0)
????????? throw new DevideByMinusException("devisor is"+y); //創建一個異常對象,并且拋出這個異常(注意這個用的是throw而不是throws.)
????? int result=x/y;
????? return result;
?? }
}
class DevideByMinusException extends Exception //創建自己的異常(除數為0時產生的異常)
{ public DevideByMinusException (String msg)
? { super(msg); }
class TestException
{ public static void main(String[] args)
? { try
??? { new Test().devide(3,-1);}
??? catch(ArithmeticException ex)? //算術異常
??? { System.out.println("program is running into ArithMetic");
????? ex.printStackTrace()??? //printStackTrace將異常情況直接打在我們的屏幕上
??? }
??? catch (DevideByMinusException ex) //自定義的除數為負數的異常
?? {? System.out.println("program is running into ArithMetic");
????? ex.printStackTrace()??? //printStackTrace將異常情況直接打在我們的屏幕上
?? }
??? catch(Exception ex)??? //這個異常必須放在所有的,catch的后面
??? { System.out.println(ex.getMessage());
????? System.exit(0);? //只有使用了這條語句,才不執行finally語句,這條語句表示程序徹底結束。
??? }
???
??? finally? //不管有沒有異常發生finally語句都要執行,即使是在程序前面用了return提前返回了,finally也要被執行,不管程序發生什么情況finally語句都要被執行,只有用System.exit(0),程序徹底結束,這條語名才不執行.
?? { System.out.println("finally"); }???
??? System.out.println("program is running here!");
? }
}

***故意用異常實現程序的跳轉***
class Test
{ ....
? ....
? void fun()
? {? try
???? { if (x==0)
????????? throw new XxxException("xxx");
?????? else
????????? throw new YyyException("yyy");
??????? .....
??????? .....
????? }
????? catch (XxxException e) //如果x==0的話,程序跳轉到這里
????? { ......}
????? catch (YyyException e) //如果x!=0的話,程序跳轉到這里??
????? {.......}???????
?? }
??? .....
?}


***子類重寫父類的方法,拋出的異常,只能比父類少,不能比父類多***
class Test
{ public int devide(int x,int y) throws ArithmeticException,DevideByMinusException?
?? {? if (y<0)
????????? throw new DevideByMinusException("devisor is"+y);
????? int result=x/y;
????? return result;
?? }
}
class SubTest extends Test
{ public int devide(int x,int y) throws ArithmeticException
//這里拋出的異常只能是父類里有的,如父類方法中的全部拋出的異常,或是其中的異常,或都不寫,但一定不能比父類多。
? {.....}
?}??

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的java学习笔记(七)----异常的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。