【问题标题】:Best way to Handle Exceptions in C# Catch Block在 C# Catch 块中处理异常的最佳方法
【发布时间】:2012-08-23 23:16:29
【问题描述】:

在 C# Catch 块中处理异常的最佳方法。我别无选择,只能在 Catch 块中将错误记录到 SQL DB。但是我想知道如果在 Catch 块本身中捕获异常的最佳方法是什么?

【问题讨论】:

  • 尝试{扔; } catch(Exception ex) { //将错误记录到 DB 如果在记录到 DB 时发生错误,最好在此处完成 }
  • 视情况而定。如果日志记录引发异常,您希望发生什么?

标签: exception exception-handling try-catch


【解决方案1】:

我会创建一个单独的类来处理错误报告,并公开一个函数来处理在数据库中记录错误。
我发现本指南很有用:

http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

我过去使用的一些代码如下所示:

try{
    throw;
}
catch(Exception ex){
    LoggingClass.LogError(some paramaters,ex.tostring());
}

然后你的日志类可能看起来像

public static class LoggingClass {

    public static void LogError(some paramaters, ex.tostring()){

        //try to log to database


        //catch and report error in some other way
    }

}

我以前使用这篇文章作为参考,因为我喜欢记录到文本文件(在发生数据库错误时)然后向用户显示“好”消息的想法。

C# best practice error handling and passing error messages

【讨论】:

  • 感谢 Will...调查它,所以基本上没有尽头给它
猜你喜欢
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多