- 不要抛出“new Exception()”–>抽象的异常往往让人迷惑
- 不要只记录Exception.Message的值,还需要记录Exception.ToString()–>Exception.ToString()包含“堆栈跟踪”信息
- catch要捕捉具体异常–>好的代码只捕捉知道的异常
- using使用–>异常发生时,using能放在资源泄露
- 新异常应将原始异常作为其内部异常
- 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功能介绍