6

我有一个 try、catch 和 finally 块的功能。如果捕获到异常,则捕获该异常的某些参数,例如其错误代码、错误详细消息和消息,并将其打印到 Excel 文件中。我在下面发布相关代码:

 public void UpdateGroup(String strSiteID, String strGroup,  int row)
        {
            try
            {
                Console.WriteLine("UpdateGroup");
                Excel1.MWMClient.MWMServiceProxy.Group group = new Excel1.MWMClient.MWMServiceProxy.Group();
                group.name = "plumber";
                group.description = "he is a plumber";  
                Console.WriteLine(groupClient.UpdateGroup(strSiteID,group));
                ExcelRecorder(0, null, null, row);
            }
            catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
            {
                ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
            }
            finally
            {
                System.GC.Collect();
            }
        }



public void ExcelRecorder(int error, string detailmessage, string message, int row)
        {  
            Excel.Application xlApp = new Excel.Application();               
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTestCases_Output.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;           
                if (!string.IsNullOrEmpty(message))
                {
                    ((Range)xlWorksheet.Cells[row, "M"]).Value2 = "FAIL";
                    ((Range)xlWorksheet.Cells[row, "N"]).Value2 = error;
                    ((Range)xlWorksheet.Cells[row, "O"]).Value2 = detailmessage;
                    ((Range)xlWorksheet.Cells[row, "P"]).Value2 = message;
                }
                else
                {
                    ((Range)xlWorksheet.Cells[row, "M"]).Value2 = "PASS";
                    ((Range)xlWorksheet.Cells[row, "N"]).Value2 = "";
                    ((Range)xlWorksheet.Cells[row, "O"]).Value2 = "";
                    ((Range)xlWorksheet.Cells[row, "P"]).Value2 = "";
                }
            xlWorkbook.Save();
            xlWorkbook.Close(0,0,0);
            xlApp.Quit();
        }

问题是,早些时候我有一段代码,比如

catch(Exception ex)
{
ExcelRecorder(ex.Message);
}

那时,所有异常都被捕获。但是,后来需求发生了变化,因为我还需要捕获错误详细代码和错误详细消息。因此,我用 catch (System.ServiceModel.FaultException ex) 更改了我的 catch 块,因为它允许我获取这些参数。但是现在,某些异常并没有被 catch 块捕获。如何更改我的 catch 块以便我可以捕获所有异常?

4

6 回答 6

9

基本上有两种方式:

1:两个catch块(最具体的第一个):

catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
{
    ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
}
catch (Exception ex)
{
    // TODO: simpler error handler
}

2:catch一块带测试:

catch (Exception ex)
{
    var fault = ex as System.ServiceModel.FaultException<DefaultFaultContract>;
    if(fault != null)
    {
        ExcelRecorder(fault.Detail.ErrorCode, fault.Detail.Message,
            fault.Message, row);
    }
    // TODO: common error handling steps
}

任何一个都可以工作。第一个可能更干净,但如果你想在里面做很多常见的事情catch,第二个可能有优势。

于 2012-09-26T07:05:07.063 回答
3

添加另一个捕获区域..您可以有多个

try
{
  // stuff
}
catch (Exception1 ex}
{
  // 1 type of exception
}
catch (Exception e)
  // catch whats left
}
于 2012-09-26T07:04:18.287 回答
2
  • System.Exception是所有异常类型之母。因此,当您拥有它时,将捕获任何类型的异常。
  • 但是,当您知道代码中可能存在特定异常并且有一个带有该异常类型作为参数的 catch 块时,该块将获得更高的优先级System.Exception
于 2012-09-26T07:06:00.547 回答
1

So from what you mentioned it seems like you have

try{}
catch(FaultException ex){ExcelRecorder(ex.Message,[other params]);}

Now you can have one more catch block for all other exceptions like

catch(Exception all){// you may log}

so when a different exception arises it would not be handled by the FaultException catch but instead move into the generic exception block and you can choose to process it as you need

于 2012-09-26T07:06:49.993 回答
1

您可以执行以下操作之一:

  • catch为您感兴趣的每个异常提供单独的块
  • 用于catch Exception ex捕获所有并仅选择您感兴趣的那些
  • 捕获您感兴趣的异常系列的基异常类,如果有这样的基类(但通常没有)

通常,您要么捕获所有异常(选项 2),要么只捕获那些您真正知道如何处理的异常(选项 1)

于 2012-09-26T07:04:27.660 回答
1

对于每个预期的异常,有尽可能多的 catch 块。不要忘记抓住最具体的。最后捕获Exception该类以捕获任何剩余的异常。

如果您抓住Exception顶部,对于任何异常,此块将触发并且所有其他块将无法访问。

try
{
     // your code here
}
catch (FirstSpecificException ex)
{

}
catch (SecondSpecificException ex)
{

}
catch (NthSpecificExceptoin ex)
{

}
catch (Exception ex)
{
    // in case you might have missed anything specifc.
}
于 2012-09-26T07:16:03.397 回答