且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

捕捉应用程序异常在Windows窗体应用程序

更新时间:2023-12-06 10:58:58

在Windows窗体应用程序,当异常被任何地方扔在应用程序(在主线程或在异步调用),你可以通过注册捉住它ThreadException事件上的应用程序。通过这种方式,你可以把所有的异常以同样的方式。

In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can treat all the exceptions in the same way.

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}