捕获异常,记录并再次抛出异常是一种好习惯吗?

san*_*gam 7 c# exception-handling

出于记录的目的,我想捕获一个例外.但是因为我还需要抛出异常,然后我会再次抛出异常.例如,我会在C#中执行此操作,如下所示:

try
{
    //exception-prone code block here
}
catch(Exception ex)
{
   Logger.Log(ex);
   throw new Exception(ex);
}
Run Code Online (Sandbox Code Playgroud)

现在我很好奇这是记录目的的最佳方式吗?建议请.

Mar*_*rco 5

没有最好的记录方法。这始终取决于您的需要。

但是,如果您想减少代码并仅记录错误,则可以创建自己的事件处理程序并将其附加到特定事件。

以AppDomain.UnhandledException事件为例:

来自MSDN的演示代码:

public class Example 
{
   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Main()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : {0} \n", e.Message);
      }

          throw new Exception("2");
       }

       static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
       {
          Exception e = (Exception) args.ExceptionObject;
          Console.WriteLine("MyHandler caught : " + e.Message);
          Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以将登录放入 MyHandler 方法中并完成它。如果没有真正处理异常,您不需要滥用 catch 块。

在 Asp.Net 中,您可以重写 global.asax 中的 Application_Error 方法:

protected void Application_Error()
{
    Exception ex = Server.GetLastError();
    NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    logger.Fatal(ex);
}
Run Code Online (Sandbox Code Playgroud)

根据您的记录程序,现在可以做任何事情。在我上面的例子中,每个致命异常都会通过电子邮件发送到事件管理系统。

在我看来,关键点是 atry / catch应该处理异常。在 catch 块中记录错误就像在 catch 块中根本没有语句一样,您只是吞下了异常,而不处理它。最终只是多余的代码。