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

问题描述:

反正是有赶上那是任何地方的code抛出expections?我想捕获异常,并处理它们以类似的方式,而不是写的try catch块的每个功能。

Is there anyway to catch expections that is thrown by anywhere in the code? I would like to catch exceptions and handle them in a similar manner rather than writing try catch blocks for each functionality.

在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...
}