1. 不要抛出“new Exception()”–>抽象的异常往往让人迷惑
  2. 不要只记录Exception.Message的值,还需要记录Exception.ToString()–>Exception.ToString()包含“堆栈跟踪”信息
  3. catch要捕捉具体异常–>好的代码只捕捉知道的异常
  4. using使用–>异常发生时,using能放在资源泄露
  5. 新异常应将原始异常作为其内部异常
  6. catch抛出新异常会导致call stack丢失–>不处理异常时可以“throw;”

自定义异常

public class SQLException : Exception
{
    public SQLException(string message) : base(message)
    {
    }
}

异常处理

  • 应用程序处理未捕获的异常
static void InitUnhandledException()
{
    AppDomain.CurrentDomain.UnhandledException += Curr 大专栏  C#中的异常处理(Exception Process in C#)entDomain_UnhandledException;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
    var exception = unhandledExceptionEventArgs.ExceptionObject as Exception;
    //Log
}

异常报告–CrashReporter.NET功能介绍

01-14 02:51
查看更多