using System; using System.Linq; using System.Threading; using System.Threading.Tasks; class Program { static void Main(string[] args) { WaitAndDisplayException("From async method", ThrowMultipleAsync()); WaitAndDisplayException("From manual method", ThrowMultipleManually()); } static void WaitAndDisplayException(string name, Task t) { Console.WriteLine("Waiting for {0}", name); try { t.Wait(); Console.WriteLine("Oops... it completed without throwing"); } catch (AggregateException e) { Console.WriteLine("Thrown exception: {0}", FormatAggregate(e)); Console.WriteLine(); Console.WriteLine("Task exception: {0}", FormatAggregate(t.Exception)); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); } static string FormatAggregate(AggregateException e) { return string.Format("{0} error(s):\r\n{1}", e.InnerExceptions.Count, string.Join("\r\n", e.InnerExceptions.Select(x => x.Message))); } static async Task ThrowMultipleAsync() { Task t1 = DelayedThrow(500); Task t2 = DelayedThrow(1000); await TaskEx.WhenAll(t1, t2); } static Task ThrowMultipleManually() { Task t1 = DelayedThrow(500); Task t2 = DelayedThrow(1000); return TaskEx.WhenAll(t1, t2); } static Task DelayedThrow(int delayMillis) { return TaskEx.Run(delegate { Thread.Sleep(delayMillis); throw new Exception("Went bang after " + delayMillis); }); } }