title: Moq Throw async exception in one of tasks in call to Task.WhenAll
body: <p>I have a number of Tasks that I am passing to a Task.WhenAll call. In my tests, I am setting up the first task to throw an exception, but my call to Tasks.WhenAll is not completed for all the tasks and immediatly breaks when the exception is thrown. My assumption was that all tasks will be completed and returned when a call to Tasks.WhenAll is made, so I think that Moq is not able to make distinctions between throwing async and non-async exceptions.</p>
  <p>I want to be able to allow all the tasks to run, and once all of them are completed, I want to grab the faulted tasks and extract their exceptions and act accordingly. Is my implementation correct? Is Moq unable to cope or do I have to fix my implementation and the problem is not with Moq?</p>
  <pre><code>public async Task StartAsync(TextWriter log)
  {
      log = TextWriter.Synchronized(log);
      var processorTaks = _processors.Select(x =&gt; x.StartAsync(log)).ToList();
      Task.WhenAll(processorTaks).Wait();
      var innerExceptions = processorTaks.Where(x =&gt; x.Exception != null)
          .Select(x =&gt; x.Exception.InnerException)
          .ToList();
      if (innerExceptions.Any())
      {
          innerExceptions.AddRange(_processors.SelectMany(x =&gt; x.NotifierException).ToList());
          var formattedExceptions = innerExceptions
              .Select(ex =&gt; $&quot;{ex.Message}&lt;\br&gt;{ex.StackTrace}&quot;)
              .Distinct();
          IEnumerable&lt;Task&gt; sendEmailTaks = _alertEmailAddresses
              .Split('|')
              .Select(
                  x =&gt;
                      _smtpClient.SendMailAsync($&quot;Notifications failed with {innerExceptions.Count()} exceptions&quot;,
                          string.Join(&quot;&lt;\br&gt;&quot;, formattedExceptions), x));
          var retry = _createWebRetry();
          await retry.RetryAsync(() =&gt; Task.WhenAll(sendEmailTaks), CancellationToken.None);
      }
  }
  </code></pre>
  <p>My setup for the processors:</p>
  <pre><code>FirstProcessor.Setup(x =&gt; x.StartAsync(It.IsAny&lt;TextWriter&gt;())).Throws(new Exception(&quot;some exception happened.&quot;));
  SecondProcessor.Setup(x =&gt; x.StartAsync(It.IsAny&lt;TextWriter&gt;())).Returns(Task.FromResult(default(object)));
  </code></pre>