在catch块中未捕获异常

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


1
你可以有多个 catch 块。 - V4Vendetta
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中处理很多常见的操作,第二种方法可能会更有优势。


3

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

try
{
  // stuff
}
catch (Exception1 ex}
{
  // 1 type of exception
}
catch (Exception e)
  // catch whats left
}

2
  • System.Exception 是所有异常类型的母类。因此,当你遇到它时,将会捕获任何一种异常。
  • 但是,如果你知道在你的代码中可能出现特定的异常,并且有一个带有该异常类型作为参数的 catch 块,则该块优先级高于 System.Exception

1

从您所提到的来看,似乎您有……

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

现在你可以为所有其他异常添加一个捕获块,例如

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

因此,当出现不同的异常时,它将不会被 FaultException 捕获,而是移动到通用异常块中,您可以根据需要选择处理它。


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.
}

1

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

  • 为您感兴趣的每个异常提供单独的catch
  • 使用catch Exception ex捕获所有异常,然后仅挑选您感兴趣的异常
  • 如果存在该异常族的基类,则捕获您感兴趣的异常族的基本异常类(但通常不存在这样的基类)

一般来说,您要么捕获所有异常(选项2),要么只捕获您确实知道如何处理的异常(选项1)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接