




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
異常處理
異?;靖拍钕到y(tǒng)異常類用戶自定義異常異常處理異常轉(zhuǎn)移一、異常處理機制當方法執(zhí)行過程中出現(xiàn)錯誤而干擾了程序流程時,會拋出一個異常,即構(gòu)造出一個異常類的對象。異常類對象代表當前出現(xiàn)的一個具體異常,該對象封裝了異常的有關(guān)信息。異常分為系統(tǒng)定義異常和用戶自定義異常。異常拋出方式:系統(tǒng)定義異常-自動拋出用戶自定義異常-用throw語句拋出方法中的異常處理:捕獲異常,就地解決,并使程序繼續(xù)執(zhí)行。將異常向外轉(zhuǎn)移,即將異常拋出方法之外,由調(diào)用該方法的環(huán)境去處理。程序中的異常處理可以提高程序的健壯性二、異常與異常類Throwable類Exception類Error類自定義異常類ArrayIndexOutOfBoundsException
類NullPointerException
類...1.系統(tǒng)異常類的層次結(jié)構(gòu)2.系統(tǒng)定義的異常類Error類定義的錯誤是致命性錯誤,一般會導(dǎo)致程序停止執(zhí)行。Exception類定義的是較輕的錯誤,你可以編寫代碼來處理這類錯誤,并繼續(xù)程序的執(zhí)行。系統(tǒng)預(yù)定義異常類及含義系統(tǒng)預(yù)定義的異常類異常對應(yīng)的運行錯誤說明ClassNotFoundException類型轉(zhuǎn)換異常:如找不到要裝載的類。IllegalArgumentException非法參數(shù)異常:可用于檢查方法參數(shù)的合法性。ArrayIndexOutOfBoundsException下標越界異常:一般指數(shù)組下標越界。FileNotFoundException找不到文件異常:未找到指定的文件或目錄。IOException輸入輸出異常:在輸入輸出操作時產(chǎn)生的異常。NullPointerException空指針異常:訪問空的尚未實例化的引用型變量。ArithmeticException數(shù)學(xué)異常:如數(shù)學(xué)運算被零除等。SecurityException安全性異常:如Applet小程序要讀寫文件。3.Exception類構(gòu)造函數(shù)Exception()Exception(String異常描述)方法StringgetMessage()
-返回異常描述StringtoString()
-返回異常對象詳細信息。voidprintStackTrace()
打印異常發(fā)生的路徑,即引起異常的方法調(diào)用嵌套序列4.用戶定義異常類用戶自定義異常主要用來處理用戶程序中特定的邏輯運行錯誤。定義異常類:classMyExpextendsException{//或繼承其他異常類
...//定義新的屬性
...//重載構(gòu)造函數(shù)
...//重載原方法,或定義新方法
}例1:用戶自定義異常。下面代碼定義一個異常類,用于表示超時異常。classTimeOutExceptionextendsException
{ privateStringreason; privateStringip; privateintport;
publicTimeOutException(Strings,StringserverIP,intserverPort) { reason=s; ip=serverIP; port=serverPort; }
publicStringgetReason(){ returnreason; } publicStringgetIp(){ returnip; } publicintgetPort(){ returnport; } publicStringtoString() { return"Exception:ipis"+ip+"portis"+port+"thereasonis"+reason;
}}4.用戶定義異常類定義好異常類后,程序中就可以拋出、捕獲并處理該類型的異常publicclassExceptionTest{ publicstaticvoidmain(String[]args) {
try {
thrownewTimeOutException(”Connecttimeout”,”166.111.8.28”,80); }
catch(TimeOutExceptione) {
System.out.println(e);
} }}三、異常處理概念:警戒區(qū)-可能會引起異常的代碼段
try{
警戒區(qū)代碼(try塊) //拋出異常
}catch(ExceptTypee){ //捕獲異常 異常處理代碼 //處理異常
}
后續(xù)語句若try塊中沒有異常,則try塊執(zhí)行完,控制轉(zhuǎn)向后續(xù)語句。若try塊中出現(xiàn)異常,則控制轉(zhuǎn)向下面的異常處理部分,然后執(zhí)行后續(xù)語句。要捕獲的異常類對象1.拋出異常
(1)系統(tǒng)自動拋出異常例2:系統(tǒng)自動拋出異常。publicclassa{ publicstaticvoidmain(String[]args) { inti; int[]array={21,33,52,44,98}; try { while(true) { i=(int)(Math.random()*10); System.out.println(”下標為”+i+”的數(shù)組元素是”+array[i]);} } catch(ArrayIndexOutOfBoundsExceptione) {
System.out.println(”出現(xiàn)數(shù)組下標越界異常”);
} }}系統(tǒng)自動拋出異常。publicclassa{ publicstaticvoidmain(String[]args) { int[]array={21,33,52,44,98}; try { System.out.println(array[6]); } catch(ArrayIndexOutOfBoundsExceptione) {
System.out.println(”出現(xiàn)數(shù)組下標越界異?!?;
} }}(2)直接拋出異常(throw語句)適用于用戶自定義異常格式: 生成異常類的實例e; throwe;或: thrownew異常類構(gòu)造方法例3:直接拋出異常。publicclassThrowException{ publicstaticvoidmain(String[]args) {
try{ thrownewArithmeticException(); } catch(ArithmeticExceptione){System.out.println(e);}
try{ thrownewArrayIndexOutOfBoundsException();} catch(ArrayIndexOutOfBoundsExceptione){ System.out.println(e); } System.out.println("throwanexception"); }}(3)間接拋出異常(throws)也稱為異常轉(zhuǎn)移異??偸前l(fā)生在方法執(zhí)行過程中。當方法代碼不對異常處理時,異常會向方法外轉(zhuǎn)移。系統(tǒng)定義的異常自動向外轉(zhuǎn)移。用戶自定義的異常要轉(zhuǎn)移需要在方法頭聲明一下例4:間接拋出異常(throws)。publicclassThrowsException{ publicstaticvoidmain(String[]args)throwsRuntimeException {
try{ thrownewArithmeticException(); } catch(ArithmeticExceptione) { System.out.println("throwexception:"+e); } throwsException();
System.out.println("throwsexception");//沒有執(zhí)行 }
staticvoidthrowsException()throwsRuntimeException { try{thrownewArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsExceptione) { System.out.println("throwexception:"+e);
thrownewArrayIndexOutOfBoundsException(); } }}(4)檢查型異常處理(5)運行時異常運行時異常都繼承自RuntimeException類publicdoubledivide(inta,intb){returna/b;}try{ //可能出現(xiàn)異常的代碼}catch(異常類型1e1){ //處理異常類型1}catch(異常類型2e2){ //處理異常類型2}……catch(異常類型3e3){ //處理異常類型3}finally{ //該代碼塊在try塊執(zhí)行后完成必做的事情}2.異常的捕獲與處理例5:從鍵盤讀取兩個整數(shù),用第一個整數(shù)除以第二個整數(shù),并輸出結(jié)果。如果在程序讀取數(shù)據(jù)的過程中發(fā)生異常,則將捕獲該異常并處理。importjava.io.*;publicclassCatchException{ publicstaticintgetInteger() { BufferedReaderinput=new BufferedReader(newInputStreamReader(System.in));
try{returnInteger.parseInt(input.readLine().trim()); } catch(Exceptione){ e.printStackTrace();//輸出異常的方法調(diào)用棧 return0; } } publicstaticvoidmain(String[]args) { intn,d,r; System.out.println("EnteroneInteger:"); n=getInteger(); System.out.println("EnteranotherInteger:"); d=getInteger();
try{ r=n/d; System.out.println(n+"/"+d+"="+r); } catch(ArithmeticExceptione){e.printStackTrace();} System.out.println("endofprogram"); }}3.多異常處理try{...}//可處理多種異常catch(異常類1e1){...}catch(異常類2e2){...}滿足異常匹配的條件:拋出對象與catch參數(shù)類型相同拋出對象為catch參數(shù)類的子類多異常處理中的匹配順序:按先后順序捕獲(注意catch塊書寫時的排列順序:先具體、后一般),但只捕獲一次。例6:使用多重catch語句classMultiCatch{publicstaticvoidmain(Stringargs[]){try{inta=args.length;System.out.println("a="+a);intb=42/a;intc[]={1};c[42]=99;}catch(ArithmeticExceptione){System.out.println("Divideby0:"+e);}catch(ArrayIndexOutOfBoundsExceptione){System.out.println("Arrayindexoob:"+e);}System.out.println("Aftertry/catchblocks.");}}練習:分析程序運行結(jié)果classF1{publicstaticvoidmain(Stringargs[]){try{inta=0;System.out.println("a="+a);intb=42/a;intc[]={1};c[42]=99;}catch(ArithmeticExceptione){System.out.println("Divideby0");}catch(ArrayIndexOutOfBoundsExceptione){System.out.println("Arrayindexoob");}System.out.println("Aftertry/catchblocks.");}}例7:異常處理例-未作異常處理publicclassTest{publicstaticvoidmain(String[]args){TestExceptionte=newTestException();te.m1();//調(diào)用m1方法,對m1方法的異常未做處理
}}classTestException{
privateinti;privateint[]array={1,2,3,4,5};//定義一個含5個元素的數(shù)組
voidm1(){//該方法中會出現(xiàn)數(shù)組下標越界異常,且無處理
for(intj=0;j<10;j++){
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公寓安裝櫥柜合同范本
- 勞務(wù)合同范本版一
- 出租土地建設(shè)合同范本
- 加盟合同范本找
- 勞務(wù)外包個人合同范本
- 個人購買商鋪合同范本
- 代辦合同范本寫
- 住宅租賃居間合同范本
- 凱迪拉克訂購合同范本
- 2025年羧甲淀粉鈉合作協(xié)議書
- 銀行間本幣市場交易員資格考試真題模擬匯編(共586題)
- 苯乙酸安全技術(shù)說明書(msds)
- 幼兒園大班《防欺凌》教案5篇,幼兒園大班防欺凌活動教案
- 衛(wèi)生管理初中級職稱大綱
- 團員民主評議測評表
- 《當代網(wǎng)絡(luò)文學(xué)作品發(fā)展研究6300字(論文)》
- 孟氏骨折與蓋氏骨折講解學(xué)習
- GB/T 9386-2008計算機軟件測試文檔編制規(guī)范
- GB/T 25137-2010鈦及鈦合金鍛件
- 第2課《說和做》課件(共30張ppt) 部編版語文七年級下冊
- 2022年廉政談話公司紀委書記對干部任前廉潔警示談話講話范文集團國有企業(yè)國企新任職
評論
0/150
提交評論