您的位置:首页 > 其它

窗体程序捕捉异常的通用简便方法不用到处添加try-catch

2014-01-14 10:08 507 查看
static class Program {
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() {
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
Exception ex = e.Exception;
MessageBox.Show(ex.Message);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show(ex.Message);

}
}

可以结合网上记录错误日志的通用方法推荐:Log4Net

下载地址http://logging.apache.org/log4net/download_log4net.cgi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: