引用 C# WinForm catch unhandled Exception

为了在WinFormCatch所有没有HandleException, 需要实现两个delegate.

1.       用来catch所有界面线程的exception.

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

2.       用来catch其它线程中的exception, 但是不能阻止程序退出。如果要阻止程序退出需要在线程运行中处理掉Exception.

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

方法实现:

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

    MessageBox.Show("CurrentDomain_UnhandledException " + e.ExceptionObject);

}

 

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{

MessageBox.Show("Application_ThreadException " + e.Exception.Message);

}

原文地址:https://www.cnblogs.com/chuncn/p/1719497.html