如何捕捉/观察从任务中抛出的未处理的异常 [英] How to catch/observe an unhandled exception thrown from a Task

查看:380
本文介绍了如何捕捉/观察从任务中抛出的未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录/报告我的应用程序(错误报告的解决方案)所有未处理的异常。我遇到一个情况,就是始终未处理。我不知道我怎么会在发生未处理的方式捕捉这个错误。请注意,我今天早上做大量的研究和尝试了很多东西..是的,我见过的这个等等。我只是在寻找一个通用的解决方案记录未处理的异常

I'm trying to log / report all unhandled exceptions in my app (error reporting solution). I've come across a scenario that is always unhandled. I'm wondering how would I catch this error in an unhandled manner. Please note that I've done a ton of research this morning and tried a lot of things.. Yes, I've seen this, this and many more. I'm just looking for a generic solution to log unhandled exceptions.

我有一个控制台应用程序的测试方法主要内部下面的代码:

I have the following code inside of a console test apps main method:

Task.Factory.StartNew(TryExecute);

Task.Run((Action)TryExecute);



以及以下方法:

as well as the following method:

private static void TryExecute() {
   throw new Exception("I'm never caught");
}



我已经尝试连接最多在我的应用程序下面,但他们从来不被称为

I'm already tried wiring up to the following in my app, but they are never called.

AppDomain.CurrentDomain.UnhandledException
TaskScheduler.UnobservedTaskException

在我的WPF应用程序,我最初发现这个错误我也连接到这些事件,但它永远不会被调用。

In my Wpf app where I initially found this error I also wired up to these events but it was never called.

Dispatcher.UnhandledException
Application.Current.DispatcherUnhandledException
System.Windows.Forms.Application.ThreadException

这是曾经被称为唯一的处理程序是:

The only handler that is called ever is:

AppDomain.CurrentDomain.FirstChanceException

但这不是一个有效的解决方案,我只想报告未捕获的异常(未都曾经执行任何catch块,每前例外,因为FirstChanceException被称为/解决。

but this is not a valid solution as I only want to report uncaught exceptions (not every exception as FirstChanceException is called before any catch blocks are ever executed / resolved.

推荐答案

TaskScheduler.UnobservedTaskException 事件应该给你你想要的东西,因为你如上所述。是什么让你认为这是不是被炒鱿鱼吗?

The TaskScheduler.UnobservedTaskException event should give you what you want, as you stated above. What makes you think that it is not getting fired?

例外是由任务捕获,然后重新抛出的但不是马上的,在具体情况。在几个方面从任务异常被重新抛出(从我的头顶,有可能更多)。

Exceptions are caught by the task and then re-thrown, but not immediately, in specific situations. Exceptions from tasks are re-thrown in several ways (off the top of my head, there are probably more).


  1. 当你尝试访问结果( Task.Result

  2. 电话等待() Task.WaitOne() Task.WaitAll()或其它相关等待的任务的方法。

  3. 当您尝试处置任务,而无需显式地看或处理异常

  1. When you try and access the result (Task.Result)
  2. Calling Wait(), Task.WaitOne(), Task.WaitAll() or another related Wait method on the task.
  3. When you try to dispose the Task without explicitly looking at or handling the exception

如果你做任何上述情况,除了将在任何线程代码运行上的和事件不会被调用重新抛出的,因为你会的观察的除外。如果没有一个尝试{}赶上{} ,你将触发 AppDomain.CurrentDomain.UnhandledException ,这听起来像什么可能发生

If you do any of the above, the exception will be rethrown on whatever thread that code is running on, and the event will not be called since you will be observing the exception. If you don't have the code inside of a try {} catch {}, you will fire the AppDomain.CurrentDomain.UnhandledException, which sounds like what might be happening.

另一种方法异常被重新抛出将是:

The other way the exception is re-thrown would be:


  • 当你没有以上,这样的任务仍然认为异常的不可观测,任务越来越敲定。它抛出最后的努力,让你知道有一个例外,你没有看到的。

如果这是表壳和自终结的不确定性,你等待GC发生使之与未观测到的异常的任务放在终结队列,然后再等着他们敲定?

If this is the case and since the finalizer is non-deterministic, are you waiting for a GC to happen so that those tasks with unobserved exceptions are put in the finalizer queue, and then waiting again for them to be finalized?

编辑:本文谈一点关于这一点。 而这篇文章有关为什么事件存在的会谈,这可能会给你深入了解就可以正常使用了。

This article talks a little bit about this. And this article talks about why the event exists, which might give you insight into how it can be used properly.

这篇关于如何捕捉/观察从任务中抛出的未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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