進行異常處理時主要涉及try、catch、finally、throw和throws關鍵字,在使用時應該注意以下幾點:
1. 不能單獨使用try、catch或finally語句塊,否則編譯出錯,例如以下代碼在編譯時間會出錯:File file=new File("D:/myfile.txt"); Try { FlieOutputStream out=new FlieOutputStream(file); out.writte("Hello!".getBytes()); // 編譯錯誤 }
2.try語句塊後既可以只使用catch語句塊,也可以只適用finally語句塊。當與catch語句塊一起使用時,可以存在多個catch語句塊,而對於finally語句塊只能存在一個。當catch與finally同時存在時,finally必須放在catch之後。
3.Try 只與finally語句塊使用時,可以使程式在發生異常後拋出異常,並繼續執行方法中的其他代碼。
public static void doFile() throws IOException{ //通過throws將異常向上拋出 File file=new File("D:/myfile.txt"); FlieOutputStream out=new FlieOutputStream(file); out.writte("start".getBytes()); //向myfile.txt中寫入資料 out.close() //關閉輸出資料流 out.writte("end".getBytes()); / / 拋出IOException異常 System.out.println("上一行代碼:out.write(null)"); //不執行該行代碼 }
對於上述代碼,如果希望異常發生後,拋出異常並繼續執行後面的代碼就可以同時使用try與finally語句塊。
public static void doFile() throws IOException{ //通過throws將異常向上拋出 File file=new File("D:/myfile.txt"); try{ File file=new File("D:/myfile.txt"); FlieOutputStream out=new FlieOutputStream(file); out.writte("start".getBytes()); //向myfile.txt中寫入資料 out.close() //關閉輸出資料流 out.writte("end".getBytes()); / / 拋出IOException異常 { System.out.println("上一行代碼:out.write(null)"); //執行該行代碼 } }
4.try 只有catch 語句塊使用時,可以使用多個catch語句塊來捕獲try語句塊中的可能發生的多種異常。異常發生後,JAVA虛擬機器會由上到下來檢測當前catch語句塊所捕獲的是否與try語句塊中發生的語句匹配,若匹配則不再執行其他的語句塊。如果多個catch語句塊捕獲的是同種類型的異常,則捕獲子類異常的catch語句塊 要放在方法的前面。
File file=new File("D:/myfile.txt"); try{ File file=new File("D:/myfile.txt"); FlieOutputStream out=new FlieOutputStream(file); out.writte("start".getBytes()); out.close() out.writte("end".getBytes()); / / 拋出IOException異常 }catch(ArithmeticException e){ System.out.println("捕獲到ArithmeticException異常"); }catch(IOException e){ //執行該catch語句塊中的代碼 System.out.println("捕獲到IOException異常"); }
下面的代碼錯誤的放置了捕獲子類與父類的catch語句塊的位置,最終導致編譯錯誤。 File file=new File("D:/myfile.txt"); try{ File file=new File("D:/myfile.txt"); FlieOutputStream out=new FlieOutputStream(file); out.writte("start".getBytes()); out.close() out.writte("end".getBytes()); / / 拋出IOException異常 }catch(IOException e){ //執行該catch語句塊中的代碼 System.out.println("捕獲到父類Exception異常"); }catch(ArithmeticException e){ //編譯出錯,異常先被Exception的catch塊捕獲,該catch塊永遠不會執行 System.out.println("捕獲到Exception的子類IOException異常"); }
5.在try語句塊中聲明的變數,只在當前try語句塊中有效,在其後的catch finally語句中塊或其他位置都不能訪問該變數。但在try-catch=finally語句塊之外聲明的變數,可以在try、catch或finally語句塊中訪問。
int age1=0; try{ age1=Integer.valueOf("20l"); //拋出NumberFormatException 異常 int age2= Integer.valueOf("24l"); //拋出NumberFormatException 異常 }catch(ArithmeticException e){ age1=-1; //編譯成功 age2=0; //編譯錯誤,無法解析age2 }finally{ System.out.println(age1); //編譯成功 System.out.println(age2); //編譯錯誤,無法解析age2 } 6.對於發生的檢查異常,必須使用try-catch語句捕獲處理,或通過throws語句向上拋出,否則編譯出錯。
7.在使用throws語句拋出一個異常對象時,該語句後面的代碼將不會被執行,例如: File file=new File("D:/myfile.txt"); Try{ FlieOutputStream out=new FlieOutputStream(file); out.writte("start".getBytes()); //向myfile.txt中寫入資料 out.close() //關閉輸出資料流 out.writte("end".getBytes()); / / 拋出IOException異常 }catch(IOException e){ Throw e; System.out.println("Throw e "); //編譯出錯,永遠執行不到的代碼 }