无法赶上UnhandledExceptions [英] Unable to catch UnhandledExceptions

查看:237
本文介绍了无法赶上UnhandledExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到试图处理的真的很烦人的问题在一个WinForms应用程序中所有未处理的异常。

I am experiencing really annoying problems trying to handle all unhandled exceptions in a Winforms application.

    [STAThread]
    [HandleProcessCorruptedStateExceptions]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.ThreadException += ApplicationOnThreadException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;

        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, false);

        CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
        Thread.CurrentThread.Name = "Main";

        LogManager.LogSoftwareStartup(Logger);

        try
        {
            new App().Run(new[] { "" });
        }
        catch (Exception e)
        {
            UnhandledExceptionHandler(e);
        }
    }



未处理的异常处理程序明显的几件事情,日志,信息,退出

The unhandled exception handler does the obvious few things, log, message, quit.

        Logger.Fatal("Unhandled exception occured : ", ex);
        MessageBox.Show("The application suffered from an unexpected error and needs to close.\nError details:\n\n" + ex.Message,
                "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        Environment.Exit(-1);

当我开始我只订阅了Application.ThreadException和AppDomain.CurrentDomain.UnhandledException。除了我添加的方式:

When I started I only subscribed to Application.ThreadException and AppDomain.CurrentDomain.UnhandledException. Along the way I added:


  • [HandleProcessCorruptedStateExceptions]

  • Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException,假);

  • 一个try / catch块周边新App.Run

当我启动应用程序(建于发布模式),通过命令脚本(命令脚本设置一些环境变量,并启动应用程序),然后抛出一个异常而不是在try catch块抓住,我得到默认未处理的异常对话框。

When I start the application (built in release mode) via command scripts (command script sets some environment variables, and starts the application), and then an exception is thrown which is not caught in a try catch block, I get the default unhandled exception dialog.

在这种情况下,我刚才扔在一个工作来重现问题更容易,我不寻求帮助摆脱例外的例外。)

In this case I just throw an exception in a Task to reproduce the problem easier, I am not looking for help to get rid of the exception :).

我要寻找的答案,是怎么回事可能仍然得到的对话框,而我已经把一切都处理对未处理的异常?!

The answer I am looking for, is how is it possible to STILL get the dialog while I have set everything to HANDLE to unhandled exception?!

所有我所采取的步骤,到目前为止都是我的想法从其他问题得到这样,但似乎没有任何帮助。

All the steps I've taken so far are ideas I get from other questions on SO, but nothing seems to help.

道歉,因为我是。被拉入另外一个会议,但想后,希望寻找答案的问题。

Apologies as I was pulled into an another meeting but wanted to post the question in the hope for an answer.

@Hans帕桑特:
我尝试了所有三个选项(自动,抓,投掷)。它在完全相同的行为的所有结果...感谢您的评论,但因为它不是我阅读MSDN文档后的预期。我期望CatchException会告诉未处理的异常处理程序实际捕获异常。 ?但无论如何,为什么我的处理程序拒绝做他们的工作。

@Hans Passant: I tried all three options (Automatic, Catch, Throw). It all results in the exact same behavior... Thanks for your comment though, because it is not what I expected after reading the documentation on MSDN. I would expect that CatchException would tell the unhandled exception handler to actually catch the exception. But anyway, why are my handlers refusing the do their job?

@Shaamaan:

@Shaamaan:

在应用程序运行(在UI线程上,命名为主),我抛出一个异常的任务,来模拟测试目的的行为。在这种情况下,在一个按钮事件处理函数(异步)

When the application runs (on the UI thread, name "Main") I throw an Exception in a task, to mimic the behavior for testing purposes. In this case, on a button event handler (async)

private async void tsbNew_Click(object sender, EventArgs e)
{
   // left out irrelevant part

   await Task.Factory.StartNew(() =>
   {
        throw new NullReferenceException();
   });
}

当任务被垃圾收集器收集,引发此异常是未处理例外,但(对我来说)不明原因的未处理的异常并没有得到我的未处理的异常处理程序捕获。

When the task is collected by the garbage collector, this exception is thrown is unhandled exception, but for (to me) unknown reasons the unhandled exception does NOT get caught by my unhandled exception handlers.

推荐答案

尽管十分一段时间过去了,也许一些Google员工可能会发现所提供的链接有用这样的回答:如果代码永远不会观察任务的例外,那么当任务消失时,TaskScheduler.UnobservedTaskException得到提高,给申请多一个机会观察除外。和如果异常仍然未观测到,该异常升级策略然后被异常终结器线程要去未处理启用。 [和扔在终结线程异常终止进程没有任何opportunaty截获并处理这个异常。请参见无法赶上未处理的异常

Although quite some time has passed, maybe some googlers might find this answer in the provided link useful: If code never observes a Task’s exception, then when the Task goes away, the TaskScheduler.UnobservedTaskException gets raised, giving the application one more opportunity to "observe" the exception. And if the exception still remains unobserved, the exception escalation policy is then enabled by the exception going unhandled on the finalizer thread. [And an exception thrown on the finalizer threads kills the process without any opportunaty to intercept and handle this exception]. See Unable to catch unhandled exceptions.

这篇关于无法赶上UnhandledExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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