2012-09-23 171 views
4

所以標題可能有點誤導,但我想完成的是讀取一個文件數組,然後將它們合併成一個,這就是我現在的位置。捕捉異常,然後拋出/發送異常並繼續

問題是,我有一個捕獲,查找異常「FileNotFoundException」,當這被稱爲我想繼續我的嘗試語句(使用「繼續」),但讓用戶知道該文件丟失。

我的設置是一個是從一種形式(它在形式,其中誤差應顯示出來)

我想到了創建一個可以從我的形式註冊的事件稱爲類,但是是正確的辦法?

public void MergeClientFiles(string directory) 
    { 
     // Find all clients 
     Array clients = Enum.GetValues(typeof(Clients)); 

     // Create a new array of files 
     string[] files = new string[clients.Length]; 

     // Combine the clients with the .txt extension 
     for (int i = 0; i < clients.Length; i++) 
      files[i] = clients.GetValue(i) + ".txt"; 

     // Merge the files into directory 
     using (var output = File.Create(directory)) 
     { 
      foreach (var file in files) 
      { 
       try 
       { 
        using (var input = File.OpenRead(file)) 
        { 
         input.CopyTo(output); 
        } 
       } 
       catch (FileNotFoundException) 
       { 
        // Its here I want to send the error to the form 
        continue; 
       } 
      } 
     } 
    } 

回答

3

您希望該方法能夠完成工作並向用戶報告問題,對嗎? 然後Oded提出了正確的事情。有了小的修改,代碼看起來是這樣的:

public List<string> MergeClientFiles(string path) 
    { 
     // Find all clients 
     Array clients = Enum.GetValues(typeof(Clients)); 

     // Create a new array of files 
     string[] files = new string[clients.Length]; 

     // Combine the clients with the .txt extension 
     for(int i = 0; i < clients.Length; i++) 
      files[i] = clients.GetValue(i) + ".txt"; 
     List<string> errors = new List<string>(); 

     // Merge the files into AllClientData 
     using(var output = File.Create(path)) { 
      foreach(var file in files) { 
       try { 
        using(var input = File.OpenRead(file)) { 
         input.CopyTo(output); 
        } 
       } 
       catch(FileNotFoundException) { 
        errors.Add(file); 
       } 
      } 
     } 
     return errors; 
    } 

然後,在主叫你只是檢查是否MergeClientFiles返回非空的集合。

+0

我想我會與這個答案一起去。這種方法有意義的方式來處理返回錯誤,並通過表單判斷是否有任何錯誤需要報告。 – Dumpen

1

您可以收集例外成List<FileNotFoundException>,並在迭代結束時,如果列表空,拋出一個自定義異常分配這個名單在對應的部件。

這將允許任何調用上述代碼來捕獲您的自定義異常,遍歷FileNotFoundException並通知用戶。

1

你可以定義一個你作爲方法參數傳遞的委託。

public delegate void FileNotFoundCallback(string file); 

public void MergeClientFiles(string directory, FileNotFoundCallback callback) 
{ 
    // Find all clients 
    Array clients = Enum.GetValues(typeof(Clients)); 

    // Create a new array of files 
    string[] files = new string[clients.Length]; 

    // Combine the clients with the .txt extension 
    for (int i = 0; i < clients.Length; i++) 
     files[i] = clients.GetValue(i) + ".txt"; 

    // Merge the files into directory 
    using (var output = File.Create(directory)) 
    { 
     foreach (var file in files) 
     { 
      try 
      { 
       using (var input = File.OpenRead(file)) 
       { 
        input.CopyTo(output); 
       } 
      } 
      catch (FileNotFoundException) 
      { 
       // Its here I want to send the error to the form 
       callback(file); 
       continue; 
      } 
     } 
    } 
} 
0

有關靈感,請查看c#中新並行結構的文檔,例如Parallel.For和Reactive Framework(rx)。

在第一個例外收集在一個AggregateException在Rx異常通過回調接口進行通信。

我想我更喜歡在Parallel.For中使用的方法,但選擇最適合您的場景的是什麼。

0

而不是趕上FileNotFoundException s你應該主動檢查文件是否存在,如果不存在則不要試圖打開它。

您可以更改該方法以返回合併文件列表,缺失文件列表或所有文件列表以及指示符(如果合併或缺失)。返回單個列表可以讓調用者選擇一次處理丟失的文件,並知道丟失了多少個文件,而不是像事件或回調那樣一個接一個地丟失。