异常处理未检测到try catch块

时间:2016-10-14 06:11:19

标签: c# wpf exception-handling

这是我的代码。

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
          }

1 个答案:

答案 0 :(得分:1)

您只是捕获特定的例外情况。如果Exception发生了您没有明确列出的程序仍然会崩溃。

当您以相同的方式处理所有异常时,您也可以切换到一般异常处理程序:

try
{
    //...
}
catch (Exception ex)
{
    fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}