Dealing with Errors

Tasks can execute arbitrary code, which obviously can also fail. How do exceptions thrown on tasks manifest themselves if they remain unhandled? Before discussing that, let’s review what happens when regular threads are used:

new Thread(() => {    throw new InvalidOperationException("Oops!");}).Start();

This causes the process to terminate because of the unhandled exception, ignoring the behavior in .NET 1.0 where this wasn’t the case. When working with threads, you must protect your threads against unhandled exceptions right inside the thread’s logic (of course, subject to the desired scrutiny): Don’t handle what you can’t deal with!

What you should remember from this is where a thread’s exceptions ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.