I have some simple code as a repro:
var taskTest = Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(5000); }).ContinueWith((Task t) => { Console.WriteLine("ERR"); }, TaskContinuationOptions.OnlyOnFaulted); try { Task.WaitAll(taskTest); } catch (AggregateException ex) { foreach (var e in ex.InnerExceptions) Console.WriteLine(e.Message + Environment.NewLine + e.StackTrace); }
However, I'm getting an unexpected TaskCanceledException being thrown in the try catch block (it's in the AggregateException InnerExceptions object). "A task was canceled".
Why am I getting this exception? The Continuation for the task never fires, there was no exception generated by it, yet I still get the aggregate exception when waiting....
I'm hoping someone can explain how this makes sense to me :)
AggregateException is used to consolidate multiple failures into a single, throwable exception object. It is used extensively in the Task Parallel Library (TPL) and Parallel LINQ (PLINQ). For more information, see Exception Handling and How to: Handle Exceptions in a PLINQ Query.
Exceptions are propagated when you use one of the static or instance Task. Wait methods, and you handle them by enclosing the call in a try / catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, multiple exceptions could be thrown.
C# 4.0 and below. You can handle exceptions using the ContinueWith overload that takes a value from the TaskContinuationOptions enumeration, like so: // Get the task. var task = Task.
You're not waiting on a task with an OnlyOnFaulted continuation - you're waiting on that continuation (returned by ContinueWith
). The continuation is never going to fire because the original task returned normally, so it's acting as if it were cancelled.
Makes sense to me.
I suspect you want to create the task, add the continuation, but then wait on the original task:
var taskTest = Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(5000); }); taskTest.ContinueWith((Task t) => { Console.WriteLine("ERR"); }, TaskContinuationOptions.OnlyOnFaulted);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With