我有一个带有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块更改为catch(System.ServiceModel.FaultException ex),因为它允许我获取这些参数。但是现在,某些异常在catch块中无法被捕获。我该如何更改我的catch块,以便可以捕获所有异常?