C#中unhandled异常处理的问题
2005-08-12 09:08
344 查看
为了增强现在正在开发的系统的健壮性,需要捕获运行时出现的无法预料而且没有被处理(unhandled)的异常。查了资料后,找到了使用 Application.ThreadException 事件处理这些异常的方法,基本步骤包括,
1、为ThreadException事件添加一个处理异常的函数句柄
2、定义处理异常的函数
例子如下:
首先需要了解的是,此时定义的事件处理函数需要在抛出异常的线程中执行,但是在主线程中给出异常提示都是在主线程中完成的,那么如何解决这个问题呢?下面的代码给出了一个比较完整的解决方案。
1、需要为所有的 AppDomain 的 UnhandledException 添加一个处理
2、 UnhandledExceptionEventArgs 参数中包含一个 IsTerminating 属性,表示是否中止 common language runtime
1、为ThreadException事件添加一个处理异常的函数句柄
2、定义处理异常的函数
例子如下:
[STAThread] static void Main() { Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); Application.Run(new FrmMain()); } private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show("Unhandled exception: "+e.Exception.ToString()); }这种方法简单易行,而且处理效率较高,可以按照用户的意图,很方便的添加处理异常理的其他功能。但我发现,如果使用第三方提供的控件时,根本不起作用,原应可能是第三方控件运行在不同的线程中。在Microsoft的帮助中也确实提到了,上面的方法只能处理主线程中未处理的异常。好了,上网查,找到了下面的方法,使用 AppDomain.UnhandledException 替代Application.ThreadException。
首先需要了解的是,此时定义的事件处理函数需要在抛出异常的线程中执行,但是在主线程中给出异常提示都是在主线程中完成的,那么如何解决这个问题呢?下面的代码给出了一个比较完整的解决方案。
private delegate void ExceptionDelegate(Exception x); static private FrmMain _MainForm; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { _MainForm = new FrmMain(); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_UnhandledException); Application.Run(_MainForm); } private static void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception exception; exception = e.ExceptionObject as Exception; if (exception == null) { // this is an unmanaged exception, you may want to handle it differently return; } PublishOnMainThread(exception); } private static void PublishOnMainThread(Exception exception) { if (_MainForm.InvokeRequired) { // Invoke executes a delegate on the thread that owns _MainForms's underlying window handle. _MainForm.Invoke(new ExceptionDelegate(HandleException), new object[] {exception}); } else { HandleException(exception); } } private static void HandleException(Exception exception) { if (SystemInformation.UserInteractive) { using (ThreadExceptionDialog dialog = new ThreadExceptionDialog(exception)) { if (dialog.ShowDialog() == DialogResult.Cancel) return; } Application.Exit(); Environment.Exit(0); } } private void ThreadMethod() { throw new Exception("From new thread"); } private void button1_Click(object sender, System.EventArgs e) { Thread thread; thread = new Thread(new ThreadStart(ThreadMethod)); thread.Start(); }需要注意的是:
1、需要为所有的 AppDomain 的 UnhandledException 添加一个处理
2、 UnhandledExceptionEventArgs 参数中包含一个 IsTerminating 属性,表示是否中止 common language runtime
相关文章推荐
- C#中unhandled异常处理的问题
- C#中unhandled异常处理的问题
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
- C#调用参数为函数指针的API函数 - 以SetUnhandledExceptionFilter为例编写一个全局异常处理程序
- Head First C# 中文版 第10章 异常处理 page455
- WinDbg 查找问题异常堆栈,堆栈跟踪UnhandledExceptionFilter
- Head First C# 中文版 第10章 异常处理 page460
- C# 异常处理-finally 和 return
- Head First C# 中文版 第10章 异常处理 page463
- C# office控件编程 书签赋值问题处理
- Head First C# 中文版 第10章 异常处理 page467
- C# 事务异常TransactionScope处理
- Asp.net 2.0的异常处理需要考虑的问题
- Head First C# 中文版 第10章 异常处理 page468
- OpenCV问题集锦,图片显示不出来,WaitKey(0),imread()不能读图片,未经处理的异常,等问题集合
- 编写高质量代码改善C#程序的157个建议[避免finaly内的无效代码、避免嵌套异常、避免吃掉异常、注意循环异常处理]
- 深入理解java异常处理机制(记得看原文评论,1楼评论解决问题或者本文PS说明)
- C#.NET--如何处理程序的未捕获异常
- 找回VisualStudio异常设置中丢失的“用户未处理的(User-unhandled)”列
- Head First C# 中文版 第10章 异常处理 page476