(java)從零開始之--異常處理(以文件拷貝爲例)

開發過程當中避免不了對異常的處理,可是異常的處理又不能亂throwjava

下面是簡單的拋異常處理數組

 1 public static void CopyFile(String souFile,String dirFile){
 2         FileInputStream inputStream = null;
 3         FileOutputStream outputStream = null;
 4         try{
 5             //找到目標文件
 6             File sou = new File(souFile);
 7             File dir = new File(dirFile);
 8             //創建文件的輸入輸出流
 9             inputStream = new FileInputStream(sou);
10             outputStream = new FileOutputStream(dir);
11             //每次讀和寫的款衝數組
12             byte[] buf = new byte[1024];
13             int len = 0;
14             //邊讀編寫
15             while((len = inputStream.read(buf))!= -1){
16                 outputStream.write(buf,0,len);
17             }
18         }catch(IOException e){
19             //爲了給調用者方便,因此就不直接拋出IOException,而是拋出一個運行時異常RuntimeException,
20             //而必須將e做爲參數傳遞進去,這樣當異常發生,調用者也能知道具體的異常信息。不然異常獲取到的
21             //RuntimeException並非調用者想要的信息。
22             throw new RuntimeException(e);
23         }finally{
24             try{
25                 //釋放資源:後創建的資源先釋放
26                 outputStream.close();
27                 inputStream.close();
28             }catch(IOException e){
29                 throw new RuntimeException(e);
30             }
31         }
32         
33     }
34     

 

這樣看似處理的還能夠,可是仍是有一點不完美,爲何?當文件路徑傳入不正確,或者說文件不存在的時候,最後的結果是spa

 Exception in thread "main" java.lang.NullPointerException
  at FileStream.Demo1.CopyFile(Demo1.java:42)
  at FileStream.Demo1.main(Demo1.java:14)指針

沒錯,就是空指針異常,當調用者的文件路徑傳入不正確的時候,上面代碼26 或 27行就會拋出空指針異常,對於一個文件拷貝操做,這樣的異常是不該該拋給調用者的,因此,在這兩行代碼中加上非空判斷,這段代碼也算作的完整了,以下:code

 1 public static void CopyFile(String souFile,String dirFile){
 2         FileInputStream inputStream = null;
 3         FileOutputStream outputStream = null;
 4         try{
 5             //找到目標文件
 6             File sou = new File(souFile);
 7             File dir = new File(dirFile);
 8             //創建文件的輸入輸出流
 9             inputStream = new FileInputStream(sou);
10             outputStream = new FileOutputStream(dir);
11             //每次讀和寫的款衝數組
12             byte[] buf = new byte[1024];
13             int len = 0;
14             //邊讀編寫
15             while((len = inputStream.read(buf))!= -1){
16                 outputStream.write(buf,0,len);
17             }
18         }catch(IOException e){
19             //爲了給調用者方便,因此就不直接拋出IOException,而是拋出一個運行時異常RuntimeException,
20             //而必須將e做爲參數傳遞進去,這樣當異常發生,調用者也能知道具體的異常信息。不然異常獲取到的
21             //RuntimeException並非調用者想要的信息。
22             throw new RuntimeException(e);
23         }finally{
24             try{
25                 //釋放資源:後創建的資源先釋放
26                 if(outputStream != null){ //這一步處理是當文件路徑不正確時,outputStream爲null,運行到此就會拋出一個空指針異常
27                     outputStream.close();
28                 }
29                 if(inputStream !=null){
30                     inputStream.close();
31                 }
32             }catch(IOException e){
33                 throw new RuntimeException(e);
34             }
35         }
36         
37     }

 

 

這樣子再一次隨便傳入一個不存在的路徑執行,獲得的結果以下:blog

 

Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: F:\a.jpg (系統找不到指定的文件。)
  at FileStream.Demo1.CopyFile(Demo1.java:38)
  at FileStream.Demo1.main(Demo1.java:14)
Caused by: java.io.FileNotFoundException: F:\a.jpg (系統找不到指定的文件。)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.<init>(FileInputStream.java:138)
  at FileStream.Demo1.CopyFile(Demo1.java:25)
  ... 1 more資源

這樣便是咱們想要獲得的信息,一目瞭然...找不到文件...開發

相關文章
相關標籤/搜索