Java異常處理原理與用法實(shí)例分析
本文實(shí)例講述了Java異常處理原理與用法。分享給大家供大家參考,具體如下:
本文內(nèi)容: 異常的介紹 處理異常 斷言首發(fā)日期:2018-03-26
異常: 異常是程序運(yùn)行中發(fā)生的錯(cuò)誤,比較常見的比如“除零異常”,如果一個(gè)除數(shù)為零,那么會發(fā)生這個(gè)異常 異常會影響程序的正常運(yùn)行,所以我們需要處理異常。
算術(shù)異常類:ArithmeticExecption
空指針異常類:NullPointerException
類型強(qiáng)制轉(zhuǎn)換異常:ClassCastException
數(shù)組下標(biāo)越界異常:ArrayIndexOutOfBoundsException
輸入輸出異常:IOException
處理異常: 異常的捕獲:try…catch…finally 格式:
public class Demo { public static void main(String[] args) {// int a=10/0; try{ int a=10/0; }catch(ArithmeticException e) { System.out.println('run in ArithmeticException '+e); //run in ArithmeticException java.lang.ArithmeticException: / by zero } catch (Exception e) { System.out.println(e); }finally { System.out.println('最終執(zhí)行的');//最終執(zhí)行的 } }} 異常的聲明:
throws用于聲明異常,聲明函數(shù)可能發(fā)生的異常。【當(dāng)函數(shù)中有throw來拋出異常時(shí),函數(shù)頭必須使用throws聲明異常】
拋出異常:throw用于手動拋出異常,可以拋出自定義異常信息:throw 異常類型(異常信息)
public class Demo2 { static int div(int a,int b) throws ArithmeticException{ if (b==0){ throw new ArithmeticException('發(fā)生除零異常了!'); } return a/b; } public static void main(String args[]) { try { System.out.println(div(2,0)); }catch(ArithmeticException e) { System.out.println(e.getMessage());//發(fā)生除零異常了! }finally { System.out.println('in finally');//in finally } System.out.println('after finally');//after finally }}
一般對于不想在函數(shù)中處理異常時(shí),一般采用異常拋出處理(throw throws);否則使用try…catch…finally捕獲異常。
自定義異常:有時(shí)候沒有定義我們想要的異常(比如我們MYSQL連接異常),那么我們可以自定義異常。
所有異常都必須是 Throwable 的子類。 如果希望寫一個(gè)檢查性異常類(是一些編譯器會幫忙檢查的異常),則需要繼承 Exception 類。 如果你想寫一個(gè)運(yùn)行時(shí)異常類(異常比如說,數(shù)組下標(biāo)越界和訪問空指針異常),那么需要繼承 RuntimeException 類【這種異常類不需要throws】。class MyException extends Exception{ public MyException() {} public MyException(String msg) { super(msg); }}public class Demo3 { static int div(int a,int b) throws MyException { if (b==0){ throw new MyException('發(fā)生異常了!'); } return a/b; } public static void main(String args[]) { try { System.out.println(div(2,0)); }catch(Exception e) { System.out.println(e);//異常.MyException: 發(fā)生除零異常了! } }}斷言: assertion(斷言)在軟件開發(fā)中是一種常用的調(diào)試方式 斷言一般是判斷條件是否符合來決定程序是否繼續(xù)執(zhí)行的。【比如,你要吃飯,那么assert一下飯到手了沒有,不然你可能會吃空氣】 斷言的開啟:eclipse、myeclipse的assert默認(rèn)是關(guān)閉,想開啟assert需要在設(shè)置perferences-》Java-》installed jres中配置一下虛擬機(jī)參數(shù),配置成-ea或者-enableassertions Java中使用assert來定義斷言:格式:assert [boolean 表達(dá)式]
public class Demo { public static void main(String[] args) { Boolean food=false; System.out.println('準(zhǔn)備開始吃飯'); assert food;System.out.println('飯來了'); }}
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. web下載文件和跳轉(zhuǎn)的方法2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. PHP函數(shù)原理理解詳談4. React+umi+typeScript創(chuàng)建項(xiàng)目的過程5. SharePoint Server 2019新特性介紹6. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析7. Vue+elementUI下拉框自定義顏色選擇器方式8. ASP的Global.asa文件技巧用法9. ASP中if語句、select 、while循環(huán)的使用方法10. JSP頁面實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
