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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java异常之try,catch,finally,throw,throws

發布時間:2024/4/14 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java异常之try,catch,finally,throw,throws 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java異常之try,catch,finally,throw,throws

你能區分異常和錯誤嗎?

我們每天上班,正常情況下可能30分鐘就能到達。但是由于車多,人多,道路擁擠,致使我們要花費更多地時間,這就是生活中的異常!

程序和生活一樣都會出現異常,先來看個異常:

?

上面出現的是算數錯誤的異常。

在java中,除去一些編譯上的錯誤(語法)之外,就有異常和錯誤!

異常的定義是可自己進行處理后,程序依然可以正常運行下去!錯誤是Java虛擬機拋出的,終止程序的運行,這就是程序和異常的區別。

?

一:什么是異常處理?

異常處理機制就像我們對平時可能遇到的意外情況,預先想好了一些處理的辦法。也就是說,在程序執行代碼的時候,萬一發生了異常,程序會按照預定的處理辦法對異常進行處理,異常處理完畢后,程序繼續運行。

java的異常處理是通過5個關鍵字來實現的:try、catch、finally、throw、throws。

?

二:java異常類的層次結構

三.常見的異常類型

Exception ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?異常層次結構的根類

ArithmeticException ? ? ? ? ? ? ? ? ? ? ? ? ? ?算數錯誤情形

ArrayIndexOutOfBoundsException ? ? ? 數組下標越界

NullPointerException ? ? ? ? ? ? ? ? ? ? ? ? ? 嘗試訪問null對象成員

ClassNotFoundException ? ? ? ? ? ? ? ? ? ? 不能加載所需的類

InputMismatchException ? ? ? ? ? ? ? ? ? ? 欲得到的數據類型與實際輸入的類型不匹配

IllegalArgumentException ? ? ? ? ? ? ? ? ? ?方法接受到非法參數

ClassCastException ? ? ? ? ? ? ? ? ? ? ? ? ? ?對象強制類型轉換出錯

NumberFormatException ? ? ? ? ? ? ? ? ? ? 數字格式轉換異常

?

四.具體實例

  • try—catch
1 package Test; 2 3 import java.util.Scanner; 4 5 public class Test_Test { 6 public static void main(String[] args) { 7 Scanner input =new Scanner(System.in); 8 System.out.println("請輸入被除數:"); 9 try { 10 int num1=input.nextInt(); 11 System.out.println("請輸入除數:"); 12 int num2=input.nextInt(); 13 System.out.println(String.format("%d / %d = %d", 14 num1, num2, num1 / num2)); 15 }catch (Exception e) { 16 System.err.println("出現錯誤:被除數和除數必須是整數,"+ 17 "除數不能為零。"); 18 System.out.println(e.getMessage()); 19 } 20 }

運行結果如下:

System.err.println();這種輸出方式可以輸出錯誤的消息,在控制臺呈現紅色。

System.out用于正常的輸出,也就是程序真正想輸出的內容。而System.err用于出錯信息的輸出,也就是你本來不期待看到的東西。

?

System.out.println(e.getMessage());

這行的作用是——返回該錯誤的詳細信息的字符串。

  • try-catch-finally
1 package Test; 2 3 import java.util.Scanner; 4 5 public class Test_Test { 6 public static void main(String[] args) { 7 Scanner input =new Scanner(System.in); 8 System.out.println("請輸入被除數:"); 9 try { 10 int num1=input.nextInt(); 11 System.out.println("請輸入除數:"); 12 int num2=input.nextInt(); 13 System.out.println(String.format("%d / %d = %d", 14 num1, num2, num1 / num2)); 15 }catch (Exception e) { 16 System.err.println("出現錯誤:被除數和除數必須是整數,"+ 17 "除數不能為零。"); 18 System.out.println(e.getMessage()); 19 } 20 finally{ 21 System.out.println("Thanks"); 22 } 23 } 24 }

運行結果如下:

try-catch-finally 程序塊的流程大致分為兩種情況:

  • 如果try塊中所有語句正常執行完畢,那么finally塊就會被執行。
  • 如果try語句在執行過程中碰到異常,無論這種異常能否被catch塊捕獲到,都將執行finally塊中的代碼。
  • try-catch-finally結構中try塊是必須有的,catch和finally塊為可選,但兩者至少必須出現其中之一。

    ?

    • try—catch-catch-finally(多重catch塊)
    1 package Test; 2 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 public class Test_Test { 7 public static void main(String[] args) { 8 Scanner input =new Scanner(System.in); 9 System.out.println("請輸入被除數:"); 10 try { 11 int num1=input.nextInt(); 12 System.out.println("請輸入除數:"); 13 int num2=input.nextInt(); 14 System.out.println(String.format("%d / %d = %d", 15 num1, num2, num1 / num2)); 16 }catch (InputMismatchException e) { 17 System.err.println("被除數和除數必須是整數。"); 18 } 19 catch (ArithmeticException e) { 20 System.err.println("除數不能為零。"); 21 } 22 catch (Exception e) { 23 System.err.println("其他未知異常。"); 24 System.out.println(e.getMessage()); 25 } 26 finally{ 27 System.out.println("Thanks"); 28 } 29 } 30 }

    運行結果如下:

    ?

    所以,在寫異常處理的時候,一定要把異常范圍小的放在前面,范圍大的放在后面,Exception這個異常的根類一定要剛在最后一個catch里面,如果放在前面或者中間,任何異常都會和Exception匹配的,就會報已捕獲到...異常的錯誤。

    ?


    ?

    下面是try-catch-finally中包含return的情況:

    • 情況一:try{} catch(){}finally{} return;

    ? ? ? ? ? ? 正常按程序順序執行即可。

    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 }

    運行結果如下:

    ?

    • 情況2:try{ return; }catch(){} finally{} return;

    ????????? 程序執行try塊中return之前(包括return語句中的表達式運算)代碼;
    ???????? 再執行finally塊,最后執行try中return;
    ???????? finally塊之后的語句return,因為程序在try中已經return所以不再執行。

    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 return 6; 15 } 16 catch (Exception e) { 17 System.out.println("我沒用!"); 18 } 19 finally 20 { 21 ++x; 22 System.out.println("我也有用!"); 23 } 24 return 2; 25 } 26 }

    運行結果如下:

    ?

    • 情況3:try{} catch(){return;} finally{} return;

    ???????? 程序先執行try,如果遇到異常執行catch塊,
    ???????? 有異常:則執行catch中return之前(包括return語句中的表達式運算)代碼,再執行finally語句中全部代碼,
    ?????????????????????最后執行catch塊中return. finally之后也就是4處的代碼不再執行。
    ?? ? ??? 無異常:執行完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("除數不能為0!"); 17 return 6; 18 } 19 finally 20 { 21 ++x; 22 System.out.println("finally"); 23 } 24 return 2; 25 } 26 }

    運行結果如下:

    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("除數不能為0!"); 16 return 6; 17 } 18 finally 19 { 20 ++x; 21 System.out.println("finally"); 22 } 23 return 2; 24 } 25 }

    ?

    運行結果如下:

    ?

    ?

    • 情況4:try{ return; }catch(){} finally{return;}

    ????????? 程序執行try塊中return之前(包括return語句中的表達式運算)代碼;
    ????????? 再執行finally塊,因為finally塊中有return所以提前退出。

    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 3; 15 } 16 catch (ArithmeticException e) { 17 System.err.println("除數不能為0!"); 18 } 19 finally 20 { 21 ++x; 22 System.out.println("finally"); 23 return 2; 24 } 25 } 26 }

    ?

    運行結果如下:

    ?

    • 情況5:try{} catch(){return;}finally{return;}

    ????????? 程序執行catch塊中return之前(包括return語句中的表達式運算)代碼;
    ????????? 再執行finally塊,因為finally塊中有return所以提前退出。

    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 } 15 catch (ArithmeticException e) { 16 System.err.println("除數不能為0!"); 17 return 4; 18 } 19 finally 20 { 21 ++x; 22 System.out.println("finally"); 23 return 2; 24 } 25 } 26 }

    運行結果如下:

    ?

    • 情況6:try{ return;}catch(){return;} finally{return;}

    ????????? 程序執行try塊中return之前(包括return語句中的表達式運算)代碼;
    ????????? 有異常:執行catch塊中return之前(包括return語句中的表達式運算)代碼;
    ???????????????????????則再執行finally塊,因為finally塊中有return所以提前退出。
    ? ? ? ??? 無異常:則再執行finally塊,因為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("try"); 14 return 4; 15 } 16 catch (ArithmeticException e) { 17 System.err.println("除數不能為0!"); 18 return 4; 19 } 20 finally 21 { 22 ++x; 23 System.out.println("finally"); 24 return 2; 25 } 26 } 27 }

    運行結果如下:

    ?

    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("除數不能為0!"); 18 return 4; 19 } 20 finally 21 { 22 ++x; 23 System.out.println("finally"); 24 return 2; 25 } 26 } 27 }

    運行結果如下:


    最終結論任何執行try 或者catch中的return語句之前,都會先執行finally語句,如果finally存在的話。
    ??????????????????如果finally中有return語句,那么程序就return了,所以finally中的return是一定會被return的,
    ??????????????????編譯器把finally中的return實現為一個warning。


    • throw——拋出異常

    拋出異常有三種形式,一是throw,一個throws,還有一種系統自動拋異常。

    系統拋出異常:

    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 }

    運行結果如下:

    ?

    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 }

    運行結果如下:

    ?

    • throws——聲明異常

    throws是方法可能拋出異常的聲明。(用在聲明方法時,表示該方法可能要拋出異常)
    語法:[(修飾符)](返回值類型)(方法名)([參數列表])[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("非數據類型不能轉換。"); 9 } 10 } 11 12 public static void Test3() throws NumberFormatException{ 13 String s = "abc"; 14 System.out.println(Double.parseDouble(s)); 15 } 16 }

    運行結果如下:

    如果在一個方法體中拋出了異常,那么我們就可以通過throws——聲明異常來通知調用者,非常方便。

    ?

    throws表示出現異常的一種可能性,并不一定會發生這些異常;throw則是拋出了異常,執行throw則一定拋出了某種異常對象。

    ?

    后說一句,try-catch-finally雖好用,但是如果是濫用,這樣只是會讓程序的可讀性變的很糟糕,當程序報錯,就無法快速準確的定位了,物盡其用 人盡其才嘛!

    轉載于:https://www.cnblogs.com/wcf6676/p/4905909.html

    總結

    以上是生活随笔為你收集整理的Java异常之try,catch,finally,throw,throws的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 免费在线观看黄网站 | www.久久久久.com | 成人av在线一区二区 | 亚洲激情六月 | av毛片精品 | 国产激情av一区二区三区 | 国产成人综合一区二区三区 | 成人免费网站www网站高清 | 欧美一级日韩 | 久久久成人精品 | 国产激情视频一区 | 国产另类在线 | 手机看片日韩国产 | 日韩精品久久一区二区 | 日本在线 | 免费看又黄又无码的网站 | 色视频导航 | 久久国产精品影院 | 国产精品天堂 | 欧美黄网站 | 午夜寂寞自拍 | 日本三级黄色大片 | 女人下面喷水视频 | 青青草在线免费视频 | 黄色美女大片 | 人禽高h交| 国产一区二区福利 | 乳罩脱了喂男人吃奶视频 | 欧美黑人做爰爽爽爽 | 2019最新中文字幕 | 免费福利影院 | 黄色免费在线网址 | 久久精品亚洲精品国产欧美 | 黄色在线免费视频 | 国产一区二区免费视频 | 久久艹国产精品 | 日本色婷婷 | 国产精品videos| 成熟人妻av无码专区 | 91成人久久 | 天天舔日日操 | 黄网站免费入口 | 色xxxx| 四季av中文字幕一区 | 成人在线免费视频播放 | 黄色大片a级 | 亚洲第一成年网 | 欧美精品播放 | 中文字幕视频 | 亚洲蜜桃av一区二区 | 成年人在线观看视频免费 | 我们的生活第五季在线观看免费 | 狠狠操狠狠插 | 情侣av| 网站国产 | 国产乱码精品一品二品 | 成人免费在线观看网站 | 精品久久久久久久久久久久久久久久 | 亚洲第九十七页 | 日韩啪| 五月婷婷社区 | 婷婷激情小说网 | 欧美日韩国产精品成人 | 日日摸日日碰 | 国产91精品一区 | 高清一二三区 | 日本一区二区成人 | 午夜福利电影一区 | wwwxxx亚洲 | 91精品人妻互换一区二区 | 小箩莉末发育娇小性色xxxx | 欧美一区二区最爽乱淫视频免费看 | 国产三级91| 久久蜜桃视频 | 在线观看视频色 | av在线不卡观看 | 亚洲va视频| 亚洲网站在线观看 | 日韩123区 | 美腿丝袜一区二区三区 | 青青在线视频 | 日韩在线视频免费看 | 欧美一级大片在线观看 | 叼嘿视频91 | 麻豆视频国产精品 | 亚洲国产精品久久久久婷婷老年 | 动漫羞羞| 国产精品久久久久久久久久久久久久久 | 超碰人人91| 免费无码毛片一区二三区 | 国精产品一区一区三区在线 | 女子spa高潮呻吟抽搐 | 国产91精品久久久久久久 | 天天综合一区 | 国产精品午夜一区二区 | 国产一区二区三区在线观看视频 | 鲁丝片一区二区三区 | 麻豆av免费看 | 亚洲天堂影视 |