我有一个 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 块以便我可以捕获所有异常?