c# - 异常没有被捕获 block

标签 c# .net winforms exception try-catch

我有一个包含 try、catch 和 finally block 的函数。如果捕获到异常,那么我会捕获该异常的某些参数,例如它的错误代码、错误详细信息和消息,并将其打印在 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 block ,因为它允许我获取那些参数。但是现在,某些异常不会被捕获到 catch block 中。我如何更改我的 catch block 以便我可以捕获所有异常?

最佳答案

基本上有两种方式:

1:两个 catch block (最具体的 first):

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 block :

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

关于c# - 异常没有被捕获 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12596242/

相关文章:

c# - 为什么 PreIncrement 类型的 PrefixUnaryExpressionSyntax 允许使用 ParanthesisedExpressionsSyntax 的操作数?

c# - 在组合框中写入

c# - 旋转时调整图像大小

winforms - 将从 nuget 下载的组件添加到工具箱的推荐方法是什么?

c# - MVC OnException 返回 Json 结果以显示错误消息

c# - VS2015- "The key combination (ctrl+K ctrl+M) is bound to command (generate method) which is not currently available"

在 search.Get().Count 上引发的 C# WMI ManagementException

.net - FSharpChart 空白输出

.net - 为什么我不能为Silverlight和通常的.NET创建程序集?

c# - 在编译器时和永远生成随机值