1

Best way to Handle Exceptions in C# Catch Block.I have no other choice but to Log the error to SQL DB in Catch block.Howver i am wondering what is the best way to catch exception if caught in Catch block itself?

4

1 回答 1

0

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

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# 最佳实践错误处理和传递错误消息

于 2012-08-24T00:16:17.547 回答