How to better understand the code/statements from “Async - Handling multiple Exceptions” article?

后端 未结 2 785
庸人自扰
庸人自扰 2020-12-18 13:13

Running the following C# console app

class Program
{  static void Main(string[] args)
   {  Tst();
      Console.ReadLine();
   }
   async static Task  Tst(         


        
2条回答
  •  庸人自扰
    2020-12-18 13:42

    In response to your "Update 2", the reasoning is still the same as in svick's answer. The task contains an AggregateException, but awaiting it throws the first InnerException.

    The additional information you need is in the Task.WhenAll documentation (emphasis mine):

    If any of the supplied tasks completes in a faulted state, the returned task will also complete in a Faulted state, where its exceptions will contain the aggregation of the set of unwrapped exceptions from each of the supplied tasks.

    So that Task's exceptions will look like:

    AggregateException 
        NullReferenceException 
        ArgumentException
    

提交回复
热议问题