Let's say I have three tasks, a
, b
, and c
. All three are guaranteed to throw an exception at a random time between 1 and 5 seconds. I then write the following code:
await Task.WhenAny(a, b, c);
This will ultimately throw an exception from whichever task faults first. Since there's no try...catch
here, this exception will bubble up to some other place in my code.
What happens when the remaining two tasks throw an exception? Aren't these unobserved exceptions, which will cause the entire process to be killed? Does that mean that the only way to use WhenAny
is inside of a try...catch
block, and then somehow observe the remaining two tasks before continuing on?
Follow-up: I'd like the answer to apply both to .NET 4.5 and .NET 4.0 with the Async Targeting Pack (though clearly using TaskEx.WhenAny
in that case).
Task.WhenAny()
,await Task.WaitAny()
wouldn't even compile. – svickTask.WhenAny
andTask.WaitAny
will never throw exceptions. – porgesWhenAny
does not throw. It returns the task that caused the WhenAny to run to completion, and awaiting that task will throw. – Dave Van den Eynde