42

I'm using the TPL (Task Parallel Library) in .NET 4.0. I want to centralize the handling logic of all unhandled exceptions by using the Thread.GetDomain().UnhandledException event. However, in my application, the event is never fired for threads started with TPL code, e.g. Task.Factory.StartNew(...). The event is indeed fired if I use something like new Thread(threadStart).Start().

This MSDN article suggests to use Task.Wait() to catch the AggregateException when working with TPL, but that is not what I want because this mechanism is not "centralized" enough.

Does anyone experience same problem at all or is it just me? Do you have any solution for this?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Buu
  • 49,745
  • 5
  • 67
  • 85

3 Answers3

35

I think TaskScheduler.UnobservedTaskException Event is what you want:

Occurs when a faulted Task's unobserved exception is about to trigger exception escalation policy, which, by default, would terminate the process.

So, this event is similar to DomainUnhandledException that you mentioned in your question but occurs only for tasks.

BTW note, that unobserved-exceptions policy (yeah, this is not an unobserved exceptions, MS guys invented new word ... again), changed from .NET 4.0 to .NET 4.5. In .NET 4.0 unobserved exception leads to process termination but in .NET 4.5 - don't. This is all because new async stuff that we'll have in C# 5 and VB 11.

Sergey Teplyakov
  • 11,477
  • 34
  • 49
  • Thanks, Sergey. I'm aware of it, but for some reason it didn't work for me http://www.buunguyen.net/blog/handle-all-uncaught-exceptions-thrown-when-using-task-parallel-library.html#comment-7259 – Buu Mar 12 '12 at 19:22
  • 9
    The problem is that the `UnobservedTaskException` event is only raised when the faulted `Task` object is finalized. And object finalization is simply not guaranteed to run. – Jean Hominal Nov 01 '12 at 10:50
  • Really helpful mention that unobserved exception policy had changed in .NET 4.5 – stukselbax Oct 29 '13 at 10:14
  • I have just done some tests in .NET 4.0 and unobserved exceptions in the `Task` do not lead to process termination. – hyankov Oct 31 '17 at 08:03
21

Seems like there's no built-in way to handle this (and no answer to this question after almost 2 weeks). I already rolled out some custom code to take care of this. The solution description is pretty lengthy, so I've posted in my blog. Refer to this post if you're interested.

Update 5/7/2010: I’ve found a better way to do that, making use of task continuation. I create a class ThreadFactory that exposes the Error event which can be subscribed by a top-level handler and provides methods to start a task attached with proper continuation.
The code is posted here.

Update 4/18/2011: Post code from the blog post as per Nifle's comment.

internal class ThreadFactory
{
    public delegate void TaskError(Task task, Exception error);

    public static readonly ThreadFactory Instance = new ThreadFactory();

    private ThreadFactory() {}

    public event TaskError Error;

    public void InvokeError(Task task, Exception error)
    {
        TaskError handler = Error;
        if (handler != null) handler(task, error);
    }

    public void Start(Action action)
    {
        var task = new Task(action);
        Start(task);
    }

    public void Start(Action action, TaskCreationOptions options)
    {
        var task = new Task(action, options);
        Start(task);
    }

    private void Start(Task task)
    {
        task.ContinueWith(t => InvokeError(t, t.Exception.InnerException),
                            TaskContinuationOptions.OnlyOnFaulted |
                            TaskContinuationOptions.ExecuteSynchronously);
        task.Start();
    }
}
Buu
  • 49,745
  • 5
  • 67
  • 85
  • 1
    The updated solution works great as far as I can tell. Why do so few people have this problem? – Helge Klein Feb 28 '11 at 22:49
  • I would be very kind of you if you could include the code from your blog here also. – Nifle Apr 16 '11 at 18:26
  • @Buu Nguyen Hi, i did something based on your approach here: http://stackoverflow.com/questions/11831844/unobservedtaskexception-being-throw-but-it-is-handled-by-a-taskscheduler-unobser/11908212#11908212 Thanks a lot. Hoping C# having something better. – newway Aug 10 '12 at 19:42
12

I see two options which can be used for the purposes of centralization of exception handling in TPL: 1. Using of Unobserved Task Exception event of Task Scheduler. 2. Using of continuations for tasks with faulted state.

Using of Unobserved Task Exception event of Task Scheduler.

The task scheduler has an UnobservedTaskException event to which you can subscribe using operator +=.

  • Note 1: In the body of handler you need to make call SetObserved() on UnobservedTaskExceptionEventArgs argument to notify scheduler that exception was handled.
  • Note 2: The handler is called when the tasks have been collected by the garbage collector.
  • Note 3: If you will wait on task you will still be forced to protect waiting by try/catch block.
  • Note 4: The default policy for unhandled Task exceptions in .Net 4.0 and 4.5 is different.

Summary: This approach is good for fire-and-forget tasks and for catching of exceptions escaped from your centralized exception handling policy.

Using of continuations for tasks with faulted state.

With TPL you can attach actions to the Task using method ContinueWith() which takes attaching action and continuation option. This action will be called after the task termination and only in the cases specified by option. In particular:

    t.ContinueWith(c => { /* exception handling code */ },
                   TaskContinuationOptions.OnlyOnFaulted);

installs continuation with exception handling code to the Task t. This code will be running only in the case when Task t was terminated due to the unhandled exception.

  • Note 1: Get exception value in exception handling code. Otherwise it will be bubbled out.
  • Note 2: Exception handling code will be called immediately after Task termination.
  • Note 3: If the exception was got in exception handling code it will be considered as handled, try/catch block on task waiting will not be able to catch it.

I think that it will be better for centralized exception handling to use custom Tasks inherited from Task with exception handler added via continuation. And accompany this approach by using of Unobserved Task Exception event of Task Scheduler to catch attempts to use not customized tasks.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
ZarathustrA
  • 3,434
  • 32
  • 28