您的位置:首页 > 其它

[.net]捕获错误机制

2014-01-05 11:31 316 查看
一般获取错误用TryCatch即可,但有时候,有的却捕获不到,程序就异常奔溃,经过@灰太龙的指导,发现这段代码可行,能够捕获异常,就算不用trycatch也行。

static Program()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string strException = string.Format("{0}发生系统异常。\r\n{1}\r\n\r\n\r\n", DateTime.Now, e.ExceptionObject.ToString());
File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemException.log"), strException);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: