Why doesn't await on Task.WhenAll throw an AggregateException?

前端 未结 8 1758
我在风中等你
我在风中等你 2020-12-04 15:14

In this code:

private async void button1_Click(object sender, EventArgs e) {
    try {
        await Task.WhenAll(DoLongThingAsyncEx1(), DoLongThingAsyncEx2(         


        
相关标签:
8条回答
  • 2020-12-04 15:44

    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....

    0 讨论(0)
  • 2020-12-04 15:49

    You can traverse all tasks to see if more than one have thrown an exception:

    private async Task Example()
    {
        var tasks = new [] { DoLongThingAsyncEx1(), DoLongThingAsyncEx2() };
    
        try 
        {
            await Task.WhenAll(tasks);
        }
        catch (Exception ex) 
        {
            var exceptions = tasks.Where(t => t.Exception != null)
                                  .Select(t => t.Exception);
        }
    }
    
    private Task DoLongThingAsyncEx1()
    {
        return Task.Run(() => { throw new InvalidTimeZoneException(); });
    }
    
    private Task DoLongThingAsyncEx2()
    {
        return Task.Run(() => { throw new InvalidOperationException(); });
    }
    
    0 讨论(0)
提交回复
热议问题