抑制Application.ThreadException和AppDomain.CurrentDomain.UnhandledException [英] Suppressing Application.ThreadException and AppDomain.CurrentDomain.UnhandledException

查看:447
本文介绍了抑制Application.ThreadException和AppDomain.CurrentDomain.UnhandledException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个执行某种同步工作的WinForms应用程序。我希望它能够保持运行,尽管有任何异常发生,以便同步继续下一次尝试。因此,我已经编写了这样的应用程序,每当发生异常时,它会记录异常堆栈跟踪,执行一些诊断,然后记录诊断信息,并继续进行下一次同步尝试。



对于未捕获的异常,我为 Application.ThreadException 以及 AppDomain添加了异常处理程序。 MainDomain.UnhandledException 在我的 Main()线程:

  static void Main(string [] args)
{
Application.ThreadException + = new ThreadExceptionEventHandler(UIThreadExceptionHandler);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException + = new UnhandledExceptionEventHandler(UnhandledExceptionHandler);


Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myForm());
}


//异常处理程序
public static void UIThreadExceptionHandler(object sender,ThreadExceptionEventArgs t)
{
logger.Fatal(致命Windows窗体错误);
logStackTrace(t.Exception); //记录一些所需格式的堆栈跟踪
Application.Exit(new CancelEventArgs(true));
}

public static void UnhandledExceptionHandler(object sender,UnhandledExceptionEventArgs t)
{
logger.Fatal(致命应用程序错误);
logStackTrace((Exception)(t.ExceptionObject)); //记录一些所需格式的堆栈跟踪
Application.Exit(new CancelEventArgs(true));
}

我保持我的应用运行测试,并意识到每当 Application.ThreadException 被捕获,应用程序正确记录字符串致命Windows窗体错误,后跟异常堆栈跟踪,然后应用程序退出。



我不想让应用程序退出。我只是希望它应该记录异常堆栈跟踪并继续下一次同步尝试。在UI线程中发生的异常并不是那么严重,因为在应用程序的其他/非UI线程中也发生了同样的异常,但它并没有崩溃应用程序(正如我在日志中看到的那样)。但是当UI线程中发生同样的异常时,应用程序崩溃。

解决方案

因为它是Windows窗体应用程序Application.ThreadException被使用对于未处理的异常:此事件允许您的Windows窗体应用程序处理在Windows窗体线程中发生的其他未处理的异常。将事件处理程序附加到ThreadException事件以处理这些异常( MSDN



我认为你必须期望像UIThreadExceptionHandler这样的行为你有Application.Exit(新的CancelEventArgs(true))。退出方法说明:通知所有消息泵它们必须终止,然后在消息处理完成后关闭所有应用程序窗口。 ( MSDN



AppDomain.CurrentDomain.UnhandledException事件用于处理非UI线程异常。



编辑1:


AppDomain.CurrentDomain.UnhandledException 专门用于在系统通知用户并终止进程之前记录异常。您不能阻止终止进程(除非您使用Net 1.1)。



Application.ThreadException + UnhandledExceptionMode.CatchException
允许你保持你的UI线程活着。但是,这不是一个好主意。因此,如果要捕获线程中的异常并保持应用程序存活,则必须执行此操作,否则将try错误代码放在try-catch块中是更好的。



它在里面使用try-catch块。



你没有发出UnhandledExceptionHandler,因为它根本没有被触发,我想。 $ b

I have written a WinForms application which performs some sort of syncing work. I want it to keep running despite of any occurrence of exceptions so that syncing keeps on going with next attempts. Thus, I have written the app such a way that whenever an exception occurs, it logs the exception stack trace, performs some diagnostics, then logs the diagnostics information and continues with next syncing attempts.

For uncaught exceptions I added Exception handlers for Application.ThreadException and also for AppDomain.CurrentDomain.UnhandledException in my Main() thread:

static void Main(string[] args)
{    
    Application.ThreadException += new ThreadExceptionEventHandler(UIThreadExceptionHandler);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);


    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new myForm());
}


//exception handlers
 public static void UIThreadExceptionHandler(object sender, ThreadExceptionEventArgs t)
 {
     logger.Fatal("Fatal Windows Forms Error");
     logStackTrace(t.Exception);                    //logs the stack trace with some desired formatting  
     Application.Exit(new CancelEventArgs(true));
 }

 public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs t) 
 {
     logger.Fatal("Fatal Application Error");
     logStackTrace((Exception)(t.ExceptionObject)); //logs the stack trace with some desired formatting           
     Application.Exit(new CancelEventArgs(true));
 }

I kept my app running for testing and realized that whenever Application.ThreadException is caught, the app correctly logs the string Fatal Windows Forms Error, followed by the exception stack trace and then the application exits.

I dont want the app to exit. I just want that it should log the exception stack trace and continue with next syncing attempt. The exception that is occurring in UI thread is not that severe one, since the same exception also occurred in the other/non-UI threads of the app, but it did not crashed the app (as I can see this in logs). However when the same exception occurs in the UI thread, the app crashes.

解决方案

As it's windows forms app Application.ThreadException is used for unhanded exception: "This event allows your Windows Forms application to handle otherwise unhandled exceptions that occur in Windows Forms threads. Attach your event handlers to the ThreadException event to deal with these exception" (MSDN)

I think you must expect such behavior as at UIThreadExceptionHandler you have Application.Exit(new CancelEventArgs(true)). Exit methods description: "Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed." (MSDN)

AppDomain.CurrentDomain.UnhandledException event is used to handle non-UI thread exceptions.

EDIT 1:

AppDomain.CurrentDomain.UnhandledException is specifically designed to log an exception before the system will inform a user and terminates the process. You can not prevent terminating the process (unless you are using Net 1.1).

Application.ThreadException + UnhandledExceptionMode.CatchException allows you to keep your UI thread alive. But, it's really not a great idea. It's much better to enclose error prone code in try-catch blocks instead.

Thus if you want to catch exceptions coming from thread and keep your application alive, you have to do it inside using try-catch block.

You haven't issue with UnhandledExceptionHandler, because it's not fired at all, I suppose.

这篇关于抑制Application.ThreadException和AppDomain.CurrentDomain.UnhandledException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆