窗体程序捕捉异常的通用简便方法不用到处添加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
/// <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
相关文章推荐
- 【RuntimeException运行时异常】方法不需要throws、调用该方法的程序不需要try-catch
- 添加/删除程序空白窗口及控制面板窗口显示异常问题的解决方法
- JAVA 处理程序异常,(try、catch、finally),(thorws)
- select元素添加option的add()方法 | try{}catch{}
- java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- [转贴]从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- throw在try中抛出异常,然后用catch捕捉并处理这个异常,同时catch也可以再次抛出这个异常
- java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- 异常----异常捕捉try&catch
- throw在try中抛出异常,然后用catch捕捉并处理这个异常,同时catch也可以再次抛出这个异常
- 异常----异常捕捉try&catch
- java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- java 异常捕捉 ( try catch finally ) 清晰解释
- java 异常捕捉 ( try catch finally ) 深度解析
- 捕捉异常中try、catch和finally的执行顺序
- java里面try,catch,finally异常捕捉
- try-catch 捕捉不到异常
- 异常----异常捕捉try&catch
- CLisp 16:抛出和捕捉异常,try-catch机制