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?
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.
I think TaskScheduler.UnobservedTaskException Event is what you want:
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.
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 +=.
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:
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.
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.