title: How to get all exceptions from all nested tasks of async/awaited parent?
body: <p>Re-using slightly modified code <a href="https://stackoverflow.com/a/16584469/2031316">from this answer</a>, how to modify it in order to implement <a href="https://stackoverflow.com/questions/16571791/how-to-better-understand-the-code-statements-from-async-handling-multiple-exc/16572968#comment23837031_16572968">@svick's advice</a>:   </p>
  
  <blockquote>
    <p>But if you save the Task into a variable and call
    task.Exception.Flatten(), that will return both exceptions  </p>
  </blockquote>
  
  <p>?  </p>
  
  <pre><code>//C# console app
    class Program
    {
      static void Main(string[] args)
      {
        Test();
        Console.ReadLine();
      }
      async static Task Test()
      {
        Task containingTask, nullRefTask, argTask;
        try
        {
          containingTask = Task.Factory.StartNew
            (() =&gt;
                {
                  nullRefTask = Task.Factory.StartNew(() =&gt;
                  {
                      throw new NullReferenceException();
                  }
                  , TaskCreationOptions.AttachedToParent);
  
                  argTask = Task.Factory.StartNew(() =&gt;
                  {
                      throw new ArgumentException();
                  }
                  , TaskCreationOptions.AttachedToParent);
                }
             );
          await containingTask;
          foreach(Exception ex in containingTask.Exception
                                                .Flatten().InnerExceptions)
          //Possible 'System.NullReferenceException'
          { //never entered
              Console.WriteLine("@@@@@"+ex.GetType().Name);
          } 
        }//try
        catch (AggregateException ex)
        {
          Console.WriteLine("** {0} **", ex.GetType().Name);
          foreach (var exc in ex.Flatten().InnerExceptions)
          {
            Console.WriteLine("$$$$$$"+exc.GetType().Name);
          }
  
  //foreach (Exception exxx in containingTask.Exception.Flatten().InnerExceptions)
  //Use of unassigned local variable 'containingTask' 
        }
  //foreach (Exception exxx in containingTask.Exception.Flatten().InnerExceptions)
  ////Use of unassigned local variable 'containingTask'   
      }
    }
  </code></pre>
  
  <p>When I am trying to access <code>containingTask.Exception.Flatten().InnerExceptions</code>  with or without using try-/catch-blocks, in  or out of <code>try</code>-, <code>catch</code>-blocks, produces errors:  </p>
  
  <pre><code>Use of unassigned local variable 'containingTask'   
  </code></pre>
  
  <p>or  </p>
  
  <pre><code>Possible 'System.NullReferenceException'
  </code></pre>
  
  <p>How to see/get both exceptions from both child tasks?  </p>
  
  <h2>Update:</h2>
  
  <p>This part was removed from question since, after correcting a typo, the code produces expected results</p>
  
  <p><s> <strong>Variant #2 (C# console app):</strong><br>
  Trying to reproduce the "workaround" from <a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/12/10/async-handling-multiple-exceptions.aspx" rel="nofollow noreferrer">"Async - Handling multiple Exceptions"</a> to see all exceptions from all child tasks:    </p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Tst();
          Console.ReadLine();
      }
      async static Task Tst()
      {
          try
          {
              Task t= Task.Factory.StartNew
                  (
                      () =&gt;
                         {
                             Task.Factory.StartNew
                             (
                                 () =&gt;
                                    { throw new NullReferenceException(); }
                                 ,TaskCreationOptions.AttachedToParent
                             );
                             Task.Factory.StartNew
                             (
                                 () =&gt;
                                     { throw new ArgumentException(); }
                                     , TaskCreationOptions.AttachedToParent
                             );
                         }
                  );
              await t.ContinueWith
                      (
                          tsk =&gt;
                            {
                                if (tsk.Exception != null)
                                     throw tsk.Exception;
                             }
                      );
          }
          catch (AggregateException ex)
          {
              foreach (var exc in ex.Flatten().InnerExceptions)
              {
                  Console.WriteLine("** {0} **", exc.GetType().Name);
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine("## {0} ##", ex.GetType().Name);
          }
      }
  }
  </code></pre>
  
  <p></s></p>