如何正确捕获WinForms应用程序中的所有未处理的异常 [英] How to catch all unhandled exceptions in WinForms application correctly

查看:84
本文介绍了如何正确捕获WinForms应用程序中的所有未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为WinForms应用程序中任何线程的所有未处理异常设置处理程序方法。我不会自己创建任何应用程序域。

I want to set the handler method for all unhandled exceptions from any threads in my WinForms application. I don't create any application domains by myself.

根据 UnhandledException 文档,我需要通过 Application设置 UnhandledExceptionMode.ThrowException 模式.SetUnhandledExceptionMode 方法也可以捕获主线程的异常:

According to UnhandledException documentation, I need to set UnhandledExceptionMode.ThrowException mode via Application.SetUnhandledExceptionMode method to catch main thread's exceptions too:


在使用Windows窗体的应用程序中,未处理的异常
主应用程序线程导致引发
的Application.ThreadException事件。如果处理了此事件,则默认行为是
未处理的异常不会终止应用程序,尽管
应用程序处于未知状态。在这种情况下,不会引发
UnhandledException事件。可以通过使用应用程序配置文件或通过使用
Application.SetUnhandledExceptionMode方法将模式更改为
UnhandledExceptionMode.ThrowException来更改此行为,方法是在ThreadException事件
处理程序发生之前迷上了。这仅适用于主应用程序
线程。对于在其他线程中抛出的未处理
异常,引发UnhandledException事件

In applications that use Windows Forms, unhandled exceptions in the main application thread cause the Application.ThreadException event to be raised. If this event is handled, the default behavior is that the unhandled exception does not terminate the application, although the application is left in an unknown state. In that case, the UnhandledException event is not raised. This behavior can be changed by using the application configuration file, or by using the Application.SetUnhandledExceptionMode method to change the mode to UnhandledExceptionMode.ThrowException before the ThreadException event handler is hooked up. This applies only to the main application thread. The UnhandledException event is raised for unhandled exceptions thrown in other threads

因此,结果代码如下所示:

So, the resulting code will look like the following:

    public static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)
    {
        // ...
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm(pathToCheck));
    }

可以吗?它会捕获任何线程(包括主线程,UI线程和 Task 类创建的所有线程)中所有未处理的异常吗?我是否正确理解了文档?

Is it ok? Will it catch all unhandled exceptions from any threads (including the main thread, UI thread and all threads created by Task class)? Did I understand the documentation correctly?

是的,我看到了诸如此处,但是我不明白为什么还要使用以下代码:

Yes, I saw questions like this here, but I don't understand why should I also use the following code:

Application.ThreadException += new     
  ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);


推荐答案

您应同时订阅两个事件。请注意,即使这样也不会自动捕获其他线程的所有内容。例如,当异步调用委托时,仅当调用 EndInvoke 时,异常才会传播到调用者线程。

You should subscribe both events. Note that even this will not catch automatically everything from other threads. For example, when delegates are invoked asynchronously, the exception will be propagated to the caller thread only when EndInvoke is called.

    [STAThread]
    static static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException +=
            (sender, args) => HandleUnhandledException(args.ExceptionObject as Exception);
        Application.ThreadException +=
            (sender, args) => HandleUnhandledException(args.Exception);
    }

    static void HandleUnhandledException(Exception e)
    {
        // show report sender and close the app or whatever
    }

这篇关于如何正确捕获WinForms应用程序中的所有未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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