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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access