Why doesn't await on Task.WhenAll throw an AggregateException?
Solution 1:
I don't exactly remember where, but I read somewhere that with new async/await keywords, they unwrap the AggregateException
into the actual exception.
So, in catch block, you get the actual exception and not the aggregated one. This helps us write more natural and intuitive code.
This was also needed for easier conversion of existing code into using async/await where the a lot of code expects specific exceptions and not aggregated exceptions.
-- Edit --
Got it:
An Async Primer by Bill Wagner
Bill Wagner said: (in When Exceptions Happen)
...When you use await, the code generated by the compiler unwraps the AggregateException and throws the underlying exception. By leveraging await, you avoid the extra work to handle the AggregateException type used by Task.Result, Task.Wait, and other Wait methods defined in the Task class. That’s another reason to use await instead of the underlying Task methods....
Solution 2:
I know this is a question that's already answered but the chosen answer doesn't really solve the OP's problem, so I thought I would post this.
This solution gives you the aggregate exception (i.e. all the exceptions that were thrown by the various tasks) and doesn't block (workflow is still asynchronous).
async Task Main()
{
var task = Task.WhenAll(A(), B());
try
{
var results = await task;
Console.WriteLine(results);
}
catch (Exception)
{
if (task.Exception != null)
{
throw task.Exception;
}
}
}
public async Task<int> A()
{
await Task.Delay(100);
throw new Exception("A");
}
public async Task<int> B()
{
await Task.Delay(100);
throw new Exception("B");
}
The key is to save a reference to the aggregate task before you await it, then you can access its Exception property which holds your AggregateException (even if only one task threw an exception).
Hope this is still useful. I know I had this problem today.