2016-10-14 10 views
-2

これは私のコードです。例外処理がcatchキャッチブロックを検出しない

void worker_DoWork(object sender, DoWorkEventArgs e) 
      { 
       try 
       { 
        foreach (var a in fpath) 
        {  
         fin = new FileStream(a, FileMode.Open, FileAccess.Read, FileShare.Read); 
          total = fin.Length; 
          totalsize += total; 
          fin.Close(); 
        } 

       foreach (var a in fpath) 
       { 

         dispatchertimer.Start(); 
         stopwatch.Start(); 

         filepath = System.IO.Path.GetFileName(a.ToString()); 
         string destFile = System.IO.Path.Combine(DestinationPath, filepath); 
         fin = new FileStream(a.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read); 
         filesize = fin.Length; 
         if (overwrite == true) 
         { 



          fout = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write); 
          while (fin.Position != filesize) 
          { 
           int n = fin.Read(buffer, 0, buffer.Length); 
           fout.Write(buffer, 0, n); 
           currentfilecopy += n; 
           PBvalue = currentfilecopy * 100/totalsize; 
           (sender as BackgroundWorker).ReportProgress((int)PBvalue); 
          } 
          fout.Flush(); 
          fout.Close(); 
          fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" }); 
         } 
         else 
         { 
          if (!File.Exists(System.IO.Path.Combine(DestinationPath, filepath))) 
          { 
           fout = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write); 
           while (fin.Position != filesize) 
           { 
            int n = fin.Read(buffer, 0, buffer.Length); 
            fout.Write(buffer, 0, n); 
            currentfilecopy += n; 
            PBvalue = currentfilecopy * 100/totalsize; 
            (sender as BackgroundWorker).ReportProgress((int)PBvalue); 
           } 
           fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" }); 
          } 
          else 
          { 

            //int n = fin.Read(buffer, 0, buffer.Length); 
            //fout.Write(buffer, 0, n); 
            filesize = fin.Length; 
            currentfilecopy += filesize; 
            PBvalue = currentfilecopy * 100/totalsize; 
            (sender as BackgroundWorker).ReportProgress((int)PBvalue); 

           fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" }); 
          } 
         } 

        } 
       } 
       #region CatchBlocks 
       catch (UnauthorizedAccessException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail",FileName = filepath }); 

        } 
        catch (ArgumentNullException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (ArgumentException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (PathTooLongException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (DirectoryNotFoundException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (FileNotFoundException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (IOException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
        catch (NotSupportedException ex) 
        { 
         fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
        } 
       #endregion 
      } 
+0

いただきましたか!? – hurnhu

+0

どのような例外が発生しますか?どのようなエラーが出ますか?例外が発生する可能性はありますか、あなたが「キャッチ」しているものとは異なるタイプですか? –

+1

catch(例外ex) { fileResultCollection.Add(新しいResult(){FileReason = ex.Message、FileResult = "Fail"、FileName = filepath}); }すべての例外の後。 –

答えて

1

特定の例外を捕捉するだけです。 Exceptionが発生した場合、プログラムを明示的にリストしなかった場合でもクラッシュします。

あなたが同じようにすべての例外を処理しているとして、あなたは同様に、一般的な例外ハンドラに切り替えることができます:

それと間違って
try 
{ 
    //... 
} 
catch (Exception ex) 
{ 
    fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath }); 
} 
関連する問題