【问题标题】:Exception not getting caught in catch block异常没有被捕获块捕获
【发布时间】:2012-09-26 07:00:41
【问题描述】:

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

【问题讨论】:

  • 你可以有多个 catch 块。

标签: c# .net winforms exception try-catch


【解决方案1】:

基本上有两种方式:

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 中做很多常见的事情,第二个可能会有优势。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      • System.Exception 是所有异常类型之母。所以当你 拥有它,将捕获任何类型的异常。
      • 但是当您知道代码中可能存在特定异常并且 有一个带有该异常类型作为参数的catch块,然后该块得到 比System.Exception 更高的优先级

      【讨论】:

        【解决方案4】:

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

        • 为您感兴趣的每个异常提供单独的 catch
        • 使用catch Exception ex 获取所有信息,只选择您感兴趣的内容
        • 捕获您感兴趣的异常族的基异常类,如果有这样的基类(但通常没有)

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

        【讨论】:

          【解决方案5】:

          所以从你提到的情况来看,你似乎有

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

          现在您可以为所有其他异常添加一个 catch 块 喜欢

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

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

          【讨论】:

            【解决方案6】:

            对于每个预期的异常都有尽可能多的 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-05-08
              • 1970-01-01
              • 2021-09-21
              • 2016-11-04
              • 2013-08-08
              • 2019-08-30
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多