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
//這里拋出的異常只能是父類里有的,如父類方法中的全部拋出的異常,或是其中的異常,或都不寫,但一定不能比父類多。
? {.....}
?}??
總結
以上是生活随笔為你收集整理的java学习笔记(七)----异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java学习笔记(六)----对象的类型
- 下一篇: java学习笔记(八)----包,jar