2012-09-23 4 views
4

제목이 약간 잘못 될 수도 있습니다.하지만 내가 원하는 것은 파일 배열을 읽은 다음 하나의 요소로 결합하여 현재 위치하는 것입니다.예외를 캐치 한 다음 예외를 던집니다/예외를 보내고 계속합니다.

문제점은 "FileNotFoundException"예외를 찾는 catch가 있는데 내 try 문 ("계속 사용")을 계속하고 싶지만 사용자에게 파일이 없다는 것을 알리면됩니다.

내 설정은 내 양식에서 등록 할 수있는 이벤트를 만드는 방법에 대해 생각

형태 (오류가 표시되어야 곳이 형태의)에서 호출되는 클래스이지만, 권리이다 방법? 목록이 비어 있지 경우

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는 옳은 것을 제안했습니다. 작은 수정을 통해, 코드는 다음과 같이 수 : MergeClientFiles이 비어 있지 않은 컬렉션을 반환하는 경우

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; 
    } 

그런 다음, 발신자에 그냥 확인.

+0

나는이 대답과 함께 갈 것이라고 생각합니다. 메소드가 오류를 리턴하고 양식이보고 할 오류가 있는지 파악할 수 있도록하는 것이 좋습니다. – Dumpen

1

당신이하는 List<FileNotFoundException>에와 반복의 끝에서 예외를 수집 할 수 있습니다, 해당 회원에게이 목록을 지정하는 사용자 정의 예외를 throw합니다.

위 코드를 호출하는 코드가 사용자 정의 예외를 catch하고 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

영감을 얻으려면 Parallel.For 및 Reactive Framework (rx)과 같은 새로운 병렬 구문에 대한 설명서를 살펴보십시오.

첫 번째 예외는 AggregateException에 수집되며, Rx 예외는 콜백 인터페이스를 통해 전달됩니다.

필자는 Parallel.For에서 사용 된 방법을 선호한다고 생각하지만 시나리오에 가장 적합한 것을 선택하십시오.

0

FileNotFoundException을 잡기보다는 파일이 존재하는지 여부를 적극적으로 확인하고 그렇지 않은 경우 열려고 시도해야합니다.

병합되거나 누락 된 경우 병합 된 파일 목록, 누락 된 파일 목록 또는 표시기와 함께 모든 파일 목록을 반환하는 방법을 변경할 수 있습니다. 단일 목록을 반환하면 누락 된 파일을 한 번에 처리하고 누락 된 파일 수를 알 수있는 옵션이 호출자에게 제공됩니다 (이벤트 또는 콜백의 경우와 같이 하나씩 실행되는 대신).

관련 문제