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 can be caught: only on ...
Get C# 4.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.