版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第五章異常處理5.1錯誤處理的方法概述5.2Java的異常處理機制5.3創(chuàng)建自己的異常類異常的演示異常(Exception):發(fā)生于程序執(zhí)行期間,表明出現了一個非法的運行狀況。許多JDK中的方法在檢測到非法情況時,會拋出一個異常對象。例如:數組越界和被0除。inti=1,j=0,k;k=i/j;5.1錯誤處理的方法概述傳統(tǒng)的程序運行時錯誤處理如C語言:函數返回值=某個可能會執(zhí)行失敗的函數();
if(函數返回值==表示該函數執(zhí)行成功的值){正常代碼} elseif(函數返回值==代表錯誤情況1的值){處理錯誤情形1} elseif(函數返回值==代表錯誤情況2的值){處理錯誤情形2} ……5.1錯誤處理的方法概述函數返回值=某個可能會執(zhí)行失敗的函數();
if(函數返回值!=表示該函數執(zhí)行成功的值)
{Switch(函數返回值){ case錯誤情況1的值:處理錯誤情形1 case錯誤情況2的值:處理錯誤情形2 ……}} else{正常代碼}5.1錯誤處理的方法概述缺點:整個程序代碼穿插錯誤處理代碼,使得條理性和可讀性差;對錯誤處理程序難以集中管理,難以保證程序的一致性;對于返回值的意義,要借助于文檔,程序維護困難。5.1錯誤處理的方法概述異常處理在進行程序設計時,錯誤的產生是不可避免的。所謂錯誤,是在程序運行過程中發(fā)生的異常事件,這些事件的發(fā)生將阻止程序的正常運行如何處理錯誤?把錯誤交給誰去處理?程序又該如何從錯誤中恢復?為了加強程序的魯棒性,Java語言具有特定的運行錯誤處理機制。5.1錯誤處理的方法概述如C++,Java語言:在異常發(fā)生時,由編程語言提供的某種機制通知應用程序,讓應用程序決定如何進行下一步的處理。傳統(tǒng)方式:負責測出錯誤的發(fā)生(程序設計者)進行錯誤的處理異常處理方式:進行錯誤的處理(程序設計者)第五章異常處理5.1錯誤處理的方法概述5.2Java的異常處理機制5.3創(chuàng)建自己的異常類5.2Java的異常處理機制錯誤有三類:語法錯誤、運行錯誤和邏輯錯誤出現語法錯誤(syntaxerror)的原因是沒有遵循語言的規(guī)則,它們可以由編譯器檢查發(fā)現。在程序運行過程中,如果環(huán)境發(fā)現了一個不可能執(zhí)行的操作,就會出現運行錯誤(runtimeerror)。如果程序沒有按照預期的方案執(zhí)行,就會發(fā)生邏輯錯誤(logicerror)。運行錯誤importjava.util.Scanner;publicclassRuntimeExceptionDemo{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("Pleaseenterainteger:");//從鍵盤讀取用戶輸入,并轉換為一個整數,//賦值給number這個變量
intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);}}如果這里出錯(如用戶輸入不是一個整數),則會發(fā)生異常。發(fā)生異常后,就會跳過后面的內容,并終止程序終止5.2Java的異常處理機制5.2Java的異常處理機制捕獲運行錯誤運行錯誤不是我們想要的,它會引起程序異常終止。需要有某種手段來捕獲這個錯誤,讓程序在收到錯誤后,能夠繼續(xù)執(zhí)行對于上一個例子來說,解決方案:
如果發(fā)現用戶輸入了錯誤的內容(如輸入的不是一個整數),則提醒用戶再次輸入,直到正確為止5.2Java的異常處理機制--改進版importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}如果在該行出現異常,在try塊中的其他部分被跳過,并轉到catch塊跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}開始接收用戶輸入。假設用戶輸入的不是整數,如“abc”跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}發(fā)生異常,并被catch捕獲出錯點try塊中剩余代碼沒有被執(zhí)行跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}接收用戶輸入。假設用戶輸入的是整數,如100跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}跟蹤程序執(zhí)行importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");intnumber=scanner.nextInt();System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(InputMismatchExceptionex){System.out.println("Incorectinput,pleasetryagain.");scanner.nextLine();//重新開始接受輸入
}}}}nextInt()與nextLine()importjava.util.InputMismatchException;importjava.util.Scanner;publicclassRuntimeExceptionDemo2{
publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);
booleaninputIsValid=false;
while(!inputIsValid){
try{System.out.println("Pleaseenterainteger:");//intnumber=scanner.nextInt();intnumber=Integer.parseInt(scanner.nextLine());System.out.println("Yourinputis:"+number);inputIsValid=true;}
catch(NumberFormatExceptionex){System.out.println("Incorectinput,pleasetryagain.");
//
scanner.nextLine();//重新開始接受輸入}}}}5.2Java的異常處理機制異常處理的目的是依據實際情況提供不同的錯誤應對策略與手段,使程序更穩(wěn)定,更安全。異常處理的主要用途是提供準確的錯誤消息,解釋失敗的原因、位置和錯誤類型等,同時提供一定的恢復能力,盡可能地保證數據完整性不被破壞,并讓程序能繼續(xù)運行。Java使用try/catch結構來捕獲異常try{}catch(...){}程序正常邏輯異常處理代碼異常類繼承結構圖ObjectThrowableExceptionErrorRuntimeExceptionIndexOutOfBoundExceptionAWTExceptionIOExceptionClassNotFoundExceptionLinkageErrorVirtualMachineErrorAWTErrorIllegalArgumentExceptionNullPointerExceptionArithmeticExceptionOtherExceptionclasses...OtherErrorclasses...OtherRuntimeExceptionclasses...InputMismatchException系統(tǒng)錯誤ObjectThrowableExceptionErrorRuntimeExceptionIndexOutOfBoundExceptionAWTExceptionIOExceptionClassNotFoundExceptionLinkageErrorVirtualMachineErrorAWTErrorIllegalArgumentExceptionNullPointerExceptionArithmeticExceptionOtherExceptionclasses...OtherErrorclasses...OtherRuntimeExceptionclasses...InputMismatchException系統(tǒng)錯誤(systemerror)是由Java虛擬機拋出并在Error類中描述的。Error類描述內部的系統(tǒng)錯誤。這種錯誤很少發(fā)生,如果發(fā)生,除了通知用戶以及盡量穩(wěn)妥地結束程序外,幾乎什么也不能做。異常類繼承結構圖——異常ObjectThrowableExceptionErrorRuntimeExceptionIndexOutOfBoundExceptionAWTExceptionIOExceptionClassNotFoundExceptionLinkageErrorVirtualMachineErrorAWTErrorIllegalArgumentExceptionNullPointerExceptionArithmeticExceptionOtherExceptionclasses...OtherErrorclasses...OtherRuntimeExceptionclasses...InputMismatchException異常(Exception)描述由程序和外部環(huán)境引起的錯誤,這些錯誤能夠通過程序捕獲和處理。運行時異常(RuntimeException)ObjectThrowableExceptionErrorRuntimeExceptionIndexOutOfBoundExceptionAWTExceptionIOExceptionClassNotFoundExceptionLinkageErrorVirtualMachineErrorAWTErrorIllegalArgumentExceptionNullPointerExceptionArithmeticExceptionOtherExceptionclasses...OtherErrorclasses...OtherRuntimeExceptionclasses...InputMismatchException運行時異常:運行異??梢圆蛔鎏幚恚\行時系統(tǒng)會把生成的運行時異常對象交給默認的異常處理程序,在標準輸出上顯示異常的內容及發(fā)生異常的位置。異常類-運行異常&編譯異常異常類-運行異常RuntimeException異常學什么能夠看懂異常信息掌握異常的兩種處理方式自定義異常了解異常體系結構和分類5.2Java的異常處理機制免檢異常(UncheckedException):RuntimeException、Error以及它們的子類都稱為免檢異常(uncheckedexception)。必檢異常(CheckedException):所有其他異常都稱為必檢異常(checkedexception),意思是指編譯器會強制程序員檢查并處理它們。5.2Java的異常處理機制ObjectThrowableExceptionErrorRuntimeExceptionIndexOutOfBoundExceptionAWTExceptionIOExceptionClassNotFoundExceptionLinkageErrorVirtualMachineErrorAWTErrorIllegalArgumentExceptionNullPointerExceptionArithmeticExceptionOtherExceptionclasses...OtherErrorclasses...OtherRuntimeExceptionclasses...InputMismatchException免檢異常:RuntimeException、Error及其子類免檢異常大多數情況下,免檢異常反映程序設計中不可捕獲的邏輯錯誤。例如,通過一個未賦值的引用變量訪問一個對象,會拋出NullPointerException異常,越界訪問一個數組的元素就會拋出IndexOutOfBoundsException異常,這些都是程序中必須糾正的邏輯錯誤。免檢異??赡茉诔绦蛉魏蔚胤匠霈F。為避免過多地使用try-catch塊,Java語言不建議編寫捕獲或聲明免檢異常的代碼。5.2Java的異常處理機制奇怪的現象publicclassTestThrows{publicstaticvoidmain(String[]args){FileInputStreamfis=newFileInputStream("a.txt");}}5.2Java的異常處理機制為什么“完全正確”的代碼不能編譯?修正錯誤publicclassTestThrows{publicstaticvoidmain(String[]args)throwsFileNotFoundException{FileInputStreamfis=newFileInputStream(“a.txt”);}}5.2Java的異常處理機制必檢異常聲明、拋出和捕獲異常method1(){
try{invokemethod2();}
catch(Exceptionex){processException;}}method2()throwsException{dosomething;
if(anerroroccours){
thrownewException();}}聲明異常拋出異常捕獲異常5.2Java的異常處理機制throws語句表明某方法中可能出現某種(或多種)異常,但它自己不能處理這些異常,而由調用者來處理。當一個方法包含throws子句時,需要在調用此方法的代碼中使用try/catch/finally進行捕獲,或者是重新對其進行聲明,否則編譯時報錯。5.2使用Java異常處理機制throws語句5.2Java的異常處理機制聲明異常每個方法都必須說明它可能拋出的必檢異常的類型,這稱為聲明異常(declaringexception)聲明異常時,使用關鍵字:throws//這里聲明的都是“必檢異?!眕ublicvoidreadFile(Stringfilename)throwsFileNotFoundException{//......}//可以聲明多個異常,用逗號分開//也可以聲明自定義異常,如
MyExceptionpublicvoidwriteFile()throwsIOException,MyException{//......}聲明拋出多個異常的方法
intg(floath)throwsOneException,TwoException{……}測試示例:ThrowMultiExceptionsDemo.java注意一個奇特的地方:當一個方法聲明拋出多個異常時,在此方法調用語句處只要catch其中任何一個異常,代碼就可以順利編譯。5.2使用Java異常處理機制拋出多個受控異常的方法5.2Java的異常處理機制throws語句中聲明的異常稱為必檢異常(checkedexception),通常直接派生自Exception類。//可以聲明多個異常,用逗號分開//也可以聲明自定義異常,如
MyExceptionpublicvoidwriteFile()throwsIOException,MyException{//......}注意:當一個方法聲明拋出多個異常時,在此方法調用語句處只要catch其中任何一個異常,代碼就可以順利編譯。拋出異常程序檢查到一個錯誤后,創(chuàng)建一個適當異常類型的實例并拋出它,這就稱為拋出異常(throwingexception)。拋出異常使用關鍵字throwpublicvoidreadFile(Stringfilename)throwsFileNotFoundException{
if(file"filename"notfound){
//方法1:
thrownewFileNotFoundException();
//方法2:
FileNotFoundExceptionex=newFileNotFoundException();
throwex;}}拋出異常舉例publicvoidsetRadius(doublenewRadius)throws
IllegalArgumentException{
if(newRadius>=0){radius=newRadius;}
else{thrownewIllegalArgumentException("Radiusmust>=0");}}IllegalArgumentException是JDK中定義的一個異常。它有一個帶有字符串參數的構造方法。Java中,異常的命名方式是:XXXException注:IllegalArgumentException是Java中的一個“免檢異?!?,是不需要進行聲明的。這里為了舉例,使用了這個異常。這種做法是不推薦的。捕獲異常try{statements.//statementsmaythrowexceptions.}catch(Exception1ex1){handleforException1}catch(Exception2ex2){handleforException2}...catch(ExceptionNexN){handleforExceptionN}5.2Java的異常處理機制捕獲異常5.2Java的異常處理機制異常的“多態(tài)”特性可以有多個catch語句塊,每個代碼塊捕獲一種異常。在某個try塊后有兩個不同的catch塊捕獲兩個相同類型的異常是語法錯誤。使用catch語句,只能捕獲Exception類及其子類的對象。因此,一個捕獲Exception對象的catch語句塊可以捕獲所有“可捕獲”的異常。將catch(Exceptione)放在別的catch塊前面會使這些catch塊都不執(zhí)行,因此,Java不會編譯這個程序。舉例:聲明、拋出和捕獲異常publicclassCircleWithException{
privatedoubleradius;
privatestaticintnumberOfObjects=0;
publicCircleWithException(){
this(1.0);}
publicCircleWithException(doublenewRadius){setRadius(newRadius);numberOfObjects++;}
publicdoublegetRadius(){
returnradius;}
publicvoidsetRadius(doublenewRadius)throwsIllegalArgumentException{
if(newRadius>=0)radius=newRadius;
else
thrownewIllegalArgumentException("Radiuscannotbenegative");}
publicstaticintgetNumberOfObjects(){
returnnumberOfObjects;}}publicclassTestCircleWithException{
publicstaticvoidmain(String[]args){
try{CircleWithExceptionc1=newCircleWithException(5);CircleWithExceptionc2=newCircleWithException(-5);CircleWithExceptionc3=newCircleWithException(0);}
catch(IllegalArgumentExceptionex){System.out.println(ex);}System.out.println("Numberofobjectscreated:"+CircleWithException.getNumberOfObjects());}}執(zhí)行程序后,輸出結果為:publicclassTestCircleWithException{
publicstaticvoidmain(String[]args){
try{CircleWithExceptionc1=newCircleWithException(5);CircleWithExceptionc2=newCircleWithException(-5);CircleWithExceptionc3=newCircleWithException(0);}
catch(IllegalArgumentExceptionex){System.out.println(ex);}System.out.println("Numberofobjectscreated:"+CircleWithException.getNumberOfObjects());}}執(zhí)行程序后,輸出結果為:java.lang.IllegalArgumentException:RadiuscannotbenegativeNumberofobjectscreated:1捕獲或聲明必檢異常Java強迫程序員處理必檢異常。如果方法聲明了一個必檢異常(即Error或Runtime-Exception之外的異常),必須:在try-catch塊中捕獲它或者在調用它的方法中再次聲明該異常例,假定方法p1調用方法p2,p2可能拋出一個必檢異常(比如:IOException)://寫法一://在p1中,使用try...catch捕獲異常//并處理之voidp1(){
try{p2();//調用p2()這個方法
}
catch(IOExceptionex){//processex}}//寫法二://在p1中,不捕獲異常,再次聲明該異常voidp1()throwsIOException{p2();//調用p2()這個方法}//p2聲明了一個IOException異常voidp2()throwsIOException{}Finally子句不論異常是否出現或者是否被捕獲,都希望執(zhí)行某些代碼。Java有一個finally子句可以用來達到這一目的。try{statements;}catch(TheExceptionex){handlingex;}finally{finalStatements;}例子1:try{statements;}catch(TheExceptionex){handlingex;}finally
{finalStatements;}Nextstatetments假設語句中沒有異常例子1:try{statements;}catch(TheExceptionex){handlingex;}finally
{finalStatements;}Nextstatetmentsfinally語句總是被執(zhí)行例子1:try{statements;}catch(TheExceptionex){handlingex;}finally
{finalStatements;}Nextstatetments繼續(xù)執(zhí)行例子2:try{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally
{finalStatements;}Nextstatetmentsstatement1中沒有異常例子2:try{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally
{finalStatements;}Nextstatetments假設在statement2中發(fā)生了一個Exception1類型的異常例子2:try{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally
{finalStatements;}Nextstatetments處理異常例子2:try{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally
{finalStatements;}Nextstatetmentsfinally語句總是被執(zhí)行例子2:try{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally
{finalStatements;}Nextstatetments繼續(xù)執(zhí)行執(zhí)行例子3:try{statement1;statement2;statement3;}catch(Exception1ex1){handlingex1;}catch(Exception2ex2){handlingex2;}finally
{finalStatements;}Nextstatetmentsstatement2拋出類型為Exception2的異常例子3:try{statement1;statement2;statement3;}catch(Exception1ex1){handlingex1;}catch(Exception2ex2){handlingex2;}finally
{finalStatements;}Nextstatetments處理異常例子3:try{statement1;statement2;statement3;}catch(Exception1ex1){handlingex1;}catch(Exception2ex2){handlingex2;}finally
{finalStatements;}Nextstatetmentsfinally總是被執(zhí)行例子3:try{statement1;statement2;statement3;}catch(Exception1ex1){handlingex1;}catch(Exception2ex2){handlingex2;}finally
{finalStatements;}Nextstatetments繼續(xù)執(zhí)行例子3:try{statement1;statement2;statement3;}catch(Exception1ex1){handlingex1;}catch(Exception2ex2)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大慶2025年黑龍江大慶市讓胡路區(qū)事業(yè)單位招聘8人筆試歷年參考題庫附帶答案詳解
- 四川2025年四川省中華職業(yè)教育社招聘工作人員筆試歷年參考題庫附帶答案詳解
- 商丘2025年河南商丘師范學院招聘高層次人才筆試歷年參考題庫附帶答案詳解
- 呼倫貝爾2025年內蒙古呼倫貝爾市住房和城鄉(xiāng)建設局所屬事業(yè)單位引進人才4人筆試歷年參考題庫附帶答案詳解
- 2025三支一扶測試卷含完整答案詳解【全優(yōu)】
- 臺州2025年浙江省臺州溫嶺市面向普通高校畢業(yè)生招聘中小學教師51人筆試歷年參考題庫附帶答案詳解
- 南陽2025年河南南陽桐柏縣招聘高中教師60人(第1號)筆試歷年參考題庫附帶答案詳解
- 南京2025下半年江蘇南京市六合區(qū)衛(wèi)生健康委員會所屬事業(yè)單位招聘衛(wèi)技人員4人筆試歷年參考題庫附帶答案詳解
- 安全員A證考試綜合提升練習試題及答案詳解參考
- 2026年醫(yī)生資格考試復習手冊模擬題篇
- CNAS-CL05-2009 實驗室生物安全認可準則
- 2024-2025學年湖北省新高考聯(lián)考協(xié)作體高一上學期12月聯(lián)考生物B及答案
- 攻擊面管理技術應用指南 2024
- 波形護欄施工質量控制方案
- 電梯井道腳手架搭設方案
- DL∕T 622-2012 立式水輪發(fā)電機彈性金屬塑料推力軸瓦技術條件
- 傳染病學-病毒性肝炎
- 重慶市沙坪壩小學小學語文五年級上冊期末試卷
- 陶瓷巖板應用技術規(guī)程
- 中藥制劑技術中職PPT完整全套教學課件
- 龍虎山正一日誦早晚課
評論
0/150
提交評論