建议65:总是处理未捕获的异常
2016-09-07 10:49
295 查看
建议65:总是处理未捕获的异常
处理未捕获的异常是每个应用程序具备的基本功能,C#在APPDomain提供了UnhandledException事件来接收未捕获到的异常的通知。常见的应用如下:
未捕获异常通常就是运行时期的Bug,我们可以在AppDomain.CurrentDomain.UnhandledException的注册事件方法CurrentDomain_UnhandledException中,将未捕获的异常信息记录在日志中。UnhandledException提供的机制并不能阻止应用程序终止,也就是说,执行CurrentDomain_UnhandledException方法后,应用程序就会终止。
在WinForm程序中使用ThreadException事件来处理UI线程异常,使用UnhandledException事件来处理非UI线程异常。ThreadException可以阻止应用程序终止。
转自:《编写高质量代码改善C#程序的157个建议》陆敏技
处理未捕获的异常是每个应用程序具备的基本功能,C#在APPDomain提供了UnhandledException事件来接收未捕获到的异常的通知。常见的应用如下:
static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception error = (Exception)e.ExceptionObject; Console.WriteLine("MyHandler caught : " + error.Message); }
未捕获异常通常就是运行时期的Bug,我们可以在AppDomain.CurrentDomain.UnhandledException的注册事件方法CurrentDomain_UnhandledException中,将未捕获的异常信息记录在日志中。UnhandledException提供的机制并不能阻止应用程序终止,也就是说,执行CurrentDomain_UnhandledException方法后,应用程序就会终止。
在WinForm程序中使用ThreadException事件来处理UI线程异常,使用UnhandledException事件来处理非UI线程异常。ThreadException可以阻止应用程序终止。
转自:《编写高质量代码改善C#程序的157个建议》陆敏技
相关文章推荐
- 编写高质量代码改善C#程序的157个建议——建议65:总是处理未捕获的异常
- 程序异常总是捕获不到未处理异常问题
- 全局异常捕获处理
- Java经验点滴:处理没有被捕获的异常
- (引用).Net 下未捕获异常的处理
- asp.net和ajax未捕获异常处理机制
- 关于SQLserver2005事务和异常处理的联合处理以及在c#中的异常捕获
- 异常处理的几个语言无关的建议
- [零基础学JAVA]Java SE面向对象部分-20.异常的捕获与处理 推荐
- 异常的捕获和处理
- c++异常处理,异常声明,捕获异常
- 关于异常处理的建议
- 用异常来处理错误----第三节 捕获和处理异常
- 异常的捕获和处理
- 捕获asp.net下的未处理异常
- .Net 下未捕获异常的处理
- winform中捕获程序未处理的所有异常
- 体面地处理程序的未捕获异常
- 系统异常捕获处理
- 正确捕获 WCF服务调用中发生的异常及处理技巧