How to handle all unhandled exceptions when using Task Parallel Library?

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.

Leave a Comment