Java异常之try,catch,finally,throw,throws
Java異常之try,catch,finally,throw,throws
你能區(qū)分異常和錯誤嗎?
我們每天上班,正常情況下可能30分鐘就能到達(dá)。但是由于車多,人多,道路擁擠,致使我們要花費(fèi)更多地時間,這就是生活中的異常!
程序和生活一樣都會出現(xiàn)異常,先來看個異常:
?
上面出現(xiàn)的是算數(shù)錯誤的異常。
在java中,除去一些編譯上的錯誤(語法)之外,就有異常和錯誤!
異常的定義是可自己進(jìn)行處理后,程序依然可以正常運(yùn)行下去!錯誤是Java虛擬機(jī)拋出的,終止程序的運(yùn)行,這就是程序和異常的區(qū)別。
?
一:什么是異常處理?
異常處理機(jī)制就像我們對平時可能遇到的意外情況,預(yù)先想好了一些處理的辦法。也就是說,在程序執(zhí)行代碼的時候,萬一發(fā)生了異常,程序會按照預(yù)定的處理辦法對異常進(jìn)行處理,異常處理完畢后,程序繼續(xù)運(yùn)行。
java的異常處理是通過5個關(guān)鍵字來實(shí)現(xiàn)的:try、catch、finally、throw、throws。
?
二:java異常類的層次結(jié)構(gòu)
三.常見的異常類型
Exception ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?異常層次結(jié)構(gòu)的根類
ArithmeticException ? ? ? ? ? ? ? ? ? ? ? ? ? ?算數(shù)錯誤情形
ArrayIndexOutOfBoundsException ? ? ? 數(shù)組下標(biāo)越界
NullPointerException ? ? ? ? ? ? ? ? ? ? ? ? ? 嘗試訪問null對象成員
ClassNotFoundException ? ? ? ? ? ? ? ? ? ? 不能加載所需的類
InputMismatchException ? ? ? ? ? ? ? ? ? ? 欲得到的數(shù)據(jù)類型與實(shí)際輸入的類型不匹配
IllegalArgumentException ? ? ? ? ? ? ? ? ? ?方法接受到非法參數(shù)
ClassCastException ? ? ? ? ? ? ? ? ? ? ? ? ? ?對象強(qiáng)制類型轉(zhuǎn)換出錯
NumberFormatException ? ? ? ? ? ? ? ? ? ? 數(shù)字格式轉(zhuǎn)換異常
?
四.具體實(shí)例
- try—catch
運(yùn)行結(jié)果如下:
System.err.println();這種輸出方式可以輸出錯誤的消息,在控制臺呈現(xiàn)紅色。
System.out用于正常的輸出,也就是程序真正想輸出的內(nèi)容。而System.err用于出錯信息的輸出,也就是你本來不期待看到的東西。
?
System.out.println(e.getMessage());這行的作用是——返回該錯誤的詳細(xì)信息的字符串。
- try-catch-finally
運(yùn)行結(jié)果如下:
try-catch-finally 程序塊的流程大致分為兩種情況:
try-catch-finally結(jié)構(gòu)中try塊是必須有的,catch和finally塊為可選,但兩者至少必須出現(xiàn)其中之一。
?
- try—catch-catch-finally(多重catch塊)
運(yùn)行結(jié)果如下:
?
所以,在寫異常處理的時候,一定要把異常范圍小的放在前面,范圍大的放在后面,Exception這個異常的根類一定要剛在最后一個catch里面,如果放在前面或者中間,任何異常都會和Exception匹配的,就會報(bào)已捕獲到...異常的錯誤。
?
?
下面是try-catch-finally中包含return的情況:
- 情況一:try{} catch(){}finally{} return;
? ? ? ? ? ? 正常按程序順序執(zhí)行即可。
1 package Test; 2 3 public class Test_Test { 4 public static void main(String[] args) { 5 Test1(); 6 } 7 8 public static int Test1(){ 9 int x = 1; 10 try 11 { 12 x++; 13 System.out.println("我有用!"); 14 } 15 catch (Exception e) { 16 System.out.println("我沒用!"); 17 } 18 finally 19 { 20 ++x; 21 System.out.println("我也有用!"); 22 } 23 return 2; 24 } 25 }運(yùn)行結(jié)果如下:
?
- 情況2:try{ return; }catch(){} finally{} return;
????????? 程序執(zhí)行try塊中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼;
???????? 再執(zhí)行finally塊,最后執(zhí)行try中return;
???????? finally塊之后的語句return,因?yàn)槌绦蛟趖ry中已經(jīng)return所以不再執(zhí)行。
運(yùn)行結(jié)果如下:
?
- 情況3:try{} catch(){return;} finally{} return;
???????? 程序先執(zhí)行try,如果遇到異常執(zhí)行catch塊,
???????? 有異常:則執(zhí)行catch中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼,再執(zhí)行finally語句中全部代碼,
?????????????????????最后執(zhí)行catch塊中return. finally之后也就是4處的代碼不再執(zhí)行。
?? ? ??? 無異常:執(zhí)行完try再finally再return.
?
1.有異常的情況:
1 package Test; 2 3 public class Test_Test { 4 public static void main(String[] args) { 5 Test1(); 6 } 7 8 public static int Test1(){ 9 int x = 5; 10 try 11 { 12 int num=x / 0; 13 System.out.println(num); 14 } 15 catch (ArithmeticException e) { 16 System.err.println("除數(shù)不能為0!"); 17 return 6; 18 } 19 finally 20 { 21 ++x; 22 System.out.println("finally"); 23 } 24 return 2; 25 } 26 }運(yùn)行結(jié)果如下:
2.無異常的情況:
1 package Test; 2 3 public class Test_Test { 4 public static void main(String[] args) { 5 Test1(); 6 } 7 8 public static int Test1(){ 9 int x = 5; 10 try 11 { 12 System.out.println("try"); 13 } 14 catch (ArithmeticException e) { 15 System.err.println("除數(shù)不能為0!"); 16 return 6; 17 } 18 finally 19 { 20 ++x; 21 System.out.println("finally"); 22 } 23 return 2; 24 } 25 }?
運(yùn)行結(jié)果如下:
?
?
- 情況4:try{ return; }catch(){} finally{return;}
????????? 程序執(zhí)行try塊中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼;
????????? 再執(zhí)行finally塊,因?yàn)閒inally塊中有return所以提前退出。
?
運(yùn)行結(jié)果如下:
?
- 情況5:try{} catch(){return;}finally{return;}
????????? 程序執(zhí)行catch塊中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼;
????????? 再執(zhí)行finally塊,因?yàn)閒inally塊中有return所以提前退出。
運(yùn)行結(jié)果如下:
?
- 情況6:try{ return;}catch(){return;} finally{return;}
????????? 程序執(zhí)行try塊中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼;
????????? 有異常:執(zhí)行catch塊中return之前(包括return語句中的表達(dá)式運(yùn)算)代碼;
???????????????????????則再執(zhí)行finally塊,因?yàn)閒inally塊中有return所以提前退出。
? ? ? ??? 無異常:則再執(zhí)行finally塊,因?yàn)閒inally塊中有return所以提前退出。
1.有異常
1 package Test; 2 3 public class Test_Test { 4 public static void main(String[] args) { 5 Test1(); 6 } 7 8 public static int Test1(){ 9 int x = 5; 10 try 11 { 12 int num = x / 0; 13 System.out.println("try"); 14 return 4; 15 } 16 catch (ArithmeticException e) { 17 System.err.println("除數(shù)不能為0!"); 18 return 4; 19 } 20 finally 21 { 22 ++x; 23 System.out.println("finally"); 24 return 2; 25 } 26 } 27 }運(yùn)行結(jié)果如下:
?
2.無異常
1 package Test; 2 3 public class Test_Test { 4 public static void main(String[] args) { 5 Test1(); 6 } 7 8 public static int Test1(){ 9 int x = 5; 10 try 11 { 12 // int num = x / 0; 13 // System.out.println("try"); 14 return 4; 15 } 16 catch (ArithmeticException e) { 17 System.err.println("除數(shù)不能為0!"); 18 return 4; 19 } 20 finally 21 { 22 ++x; 23 System.out.println("finally"); 24 return 2; 25 } 26 } 27 }運(yùn)行結(jié)果如下:
最終結(jié)論:任何執(zhí)行try 或者catch中的return語句之前,都會先執(zhí)行finally語句,如果finally存在的話。
??????????????????如果finally中有return語句,那么程序就return了,所以finally中的return是一定會被return的,
??????????????????編譯器把finally中的return實(shí)現(xiàn)為一個warning。
- throw——拋出異常
拋出異常有三種形式,一是throw,一個throws,還有一種系統(tǒng)自動拋異常。
系統(tǒng)拋出異常:
1 package Test; 2 3 public class Test2 { 4 public static void main(String[] args) { 5 int a = 5, b =0; 6 System.out.println(5/b); 7 } 8 9 }運(yùn)行結(jié)果如下:
?
throw拋出異常:
throw是語句拋出一個異常。
語法:throw?(異常對象);
?
1 package Test; 2 3 public class Test2 { 4 public static void main(String[] args) { 5 String s = "abc"; 6 if(s.equals("abc")) { 7 throw new NumberFormatException(); 8 } else { 9 System.out.println(s); 10 } 11 } 12 13 }運(yùn)行結(jié)果如下:
?
- throws——聲明異常
throws是方法可能拋出異常的聲明。(用在聲明方法時,表示該方法可能要拋出異常)
語法:[(修飾符)](返回值類型)(方法名)([參數(shù)列表])[throws(異常類)]{......}
?
1 package Test; 2 3 public class Test2 { 4 public static void main(String[] args) { 5 try { 6 Test3(); 7 } catch (NumberFormatException e) { 8 System.err.println("非數(shù)據(jù)類型不能轉(zhuǎn)換。"); 9 } 10 } 11 12 public static void Test3() throws NumberFormatException{ 13 String s = "abc"; 14 System.out.println(Double.parseDouble(s)); 15 } 16 }
運(yùn)行結(jié)果如下:
如果在一個方法體中拋出了異常,那么我們就可以通過throws——聲明異常來通知調(diào)用者,非常方便。
?
throws表示出現(xiàn)異常的一種可能性,并不一定會發(fā)生這些異常;throw則是拋出了異常,執(zhí)行throw則一定拋出了某種異常對象。
?
最后說一句,try-catch-finally雖好用,但是如果是濫用,這樣只是會讓程序的可讀性變的很糟糕,當(dāng)程序報(bào)錯,就無法快速準(zhǔn)確的定位了,物盡其用 人盡其才嘛!
轉(zhuǎn)載于:https://www.cnblogs.com/wcf6676/p/4905909.html
總結(jié)
以上是生活随笔為你收集整理的Java异常之try,catch,finally,throw,throws的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#异步编程(一):异步基础
- 下一篇: 20145315 《Java程序设计》第