using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main()
Console.WriteLine("Starting Main()");
Task example = Example();
Console.WriteLine("Exiting Main()");
private static async Task Example()
Task[] tasks = new [] { DoLongThingAsyncEx1(), DoLongThingAsyncEx2() };
Task allTasksCompleted = Task.WhenAll(tasks);
catch (Exception singleEx)
Console.WriteLine("'await allTasksCompleted' threw exception of type " + singleEx.GetType().Name.ToString());
Console.WriteLine("allTasksCompleted.Exception is type " + allTasksCompleted.Exception.GetType().Name);
Console.WriteLine("allTasksCompleted.Exception.InnerException is type " + allTasksCompleted.Exception.InnerException.GetType().Name);
List<AggregateException> exceptions = tasks.Where(t => t.Exception != null)
.Select(t => t.Exception)
Console.WriteLine(exceptions.Count.ToString() + " exception(s) occurred across tasks[]:");
foreach(AggregateException ex in exceptions) {
Console.WriteLine("--> " + ex.GetType().Name + " [" + ex.InnerExceptions.Count.ToString() + " InnerException(s)]");
foreach(var innerEx in ex.InnerExceptions) {
Console.WriteLine(" \\--> " + innerEx.GetType().Name);
private static Task DoLongThingAsyncEx1()
return Task.Run(() => { throw new InvalidTimeZoneException(); });
private static Task DoLongThingAsyncEx2()
return Task.Run(() => { throw new InvalidOperationException(); });