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?
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.
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();
}
}
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.