我有一個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塊。 – V4Vendetta