捕捉应用程序异常在Windows窗体应用程序 [英] Catch Application Exceptions in a Windows Forms Application

查看:417
本文介绍了捕捉应用程序异常在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...
}

这篇关于捕捉应用程序异常在Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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