Java異常處理操作實例小結
本文實例講述了Java異常處理操作。分享給大家供大家參考,具體如下:
一 異常處理的使用——能夠捕獲到異常
1 代碼
public class DealException { public static void main( String[] args ) { try // 檢查這個程序塊的代碼 { int arr[] = new int[5]; arr[10] = 7; // 在這里會出現(xiàn)異常 } catch( ArrayIndexOutOfBoundsException e ) { System.out.println( "數(shù)組超出綁定范圍!" ); } finally // 這個塊的程序代碼一定會執(zhí)行 { System.out.println( "這里一定會被執(zhí)行!" ); } System.out.println( "main()方法結束!" ); } }
2 運行
數(shù)組超出綁定范圍!
這里一定會被執(zhí)行!
main()方法結束!
二 異常處理的使用——不能夠捕獲到異常
1 代碼
public class DealException { public static void main( String[] args ) { try // 檢查這個程序塊的代碼 { int arr[] = new int[5]; arr[10] = 7; // 在這里會出現(xiàn)異常 } catch( ArithmeticException e ) { System.out.println( "算術異常" ); } finally // 這個塊的程序代碼一定會執(zhí)行 { System.out.println( "這里一定會被執(zhí)行!" ); } System.out.println( "main()方法結束!" ); } }
2 運行
這里一定會被執(zhí)行!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at DealException.main(DealException.java:9)
3 說明
對于未捕獲到異常場景,finally語句塊會執(zhí)行,但finally語言塊之后的語句不能執(zhí)行,將異常交給JVM去處理,最后停止程序運行。
三 異常對象ex的使用
1 代碼
public class excepObject { public static void main( String[] args ) { try { int arr[] = new int[5]; arr[10] = 7; } catch( ArrayIndexOutOfBoundsException ex ) { System.out.println( "數(shù)組超出綁定范圍!" ); System.out.println( "異常:" + ex ); // 顯示異常對象e的內(nèi)容 // ex.printStackTrace(); } System.out.println( "main()方法結束!" ); } }
2 運行
數(shù)組超出綁定范圍!
異常:java.lang.ArrayIndexOutOfBoundsException: 10
main()方法結束!
四 通過多個catch捕獲異常
1 代碼
public class arrayException { public static void main( String args[] ) { System.out.println("-----A、計算開始之前"); try { int arr[] = new int[5]; ; arr[0] = 3; arr[1] = 6; arr[1] = 0; // 除數(shù)為0,有異常 arr[10] = 7; // 數(shù)組下標越界,有異常 int result = arr[0] / arr[1]; System.out.println("------B、除法計算結果:" + result); } catch (ArithmeticException ex) { ex.printStackTrace(); } catch (ArrayIndexOutOfBoundsException ex) { ex.printStackTrace(); } finally { System.out.println("-----此處不管是否出錯,都會執(zhí)行?。?!"); } System.out.println("-----C、計算結束之后。"); } }
2 運行
-----A、計算開始之前
java.lang.ArrayIndexOutOfBoundsException: 10
at arrayException.main(arrayException.java:19)
-----此處不管是否出錯,都會執(zhí)行?。?!
-----C、計算結束之后。
更多java相關內(nèi)容感興趣的讀者可查看本站專題:《Java面向對象程序設計入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
Java使用DFA算法實現(xiàn)過濾多家公司自定義敏感字功能詳解
這篇文章主要介紹了Java使用DFA算法實現(xiàn)過濾多家公司自定義敏感字功能,結合實例形式分析了DFA算法的實現(xiàn)原理及過濾敏感字的相關操作技巧,需要的朋友可以參考下2017-08-08Java求兩個正整數(shù)的最大公約數(shù)和最小公倍數(shù)
這篇文章主要介紹了輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù),需要的朋友可以參考下2017-02-02Java interrupt()方法使用注意_動力節(jié)點Java學院整理
這篇文章主要介紹了Java interrupt()方法使用注意_動力節(jié)點Java學院整理,需要的朋友可以參考下2017-05-05SpringBoot整合WebSocket的客戶端和服務端的實現(xiàn)代碼
這篇文章主要介紹了SpringBoot整合WebSocket的客戶端和服務端的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07Java C++ leetcode執(zhí)行一次字符串交換能否使兩個字符串相等
這篇文章主要為大家介紹了Java C++ leetcode1790執(zhí)行一次字符串交換能否使兩個字符串相等,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Java的動態(tài)綁定與雙分派_動力節(jié)點Java學院整理
這篇文章主要介紹了Java的動態(tài)綁定與雙分派,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08