using System.Threading.Tasks;
public static void Main()
TestSuccess().GetAwaiter().GetResult();
TestFail().GetAwaiter().GetResult();
TestCanceled().GetAwaiter().GetResult();
private static async Task TestSuccess()
.WhenAll(DoLongThingAsyncEx1(false), DoLongThingAsyncEx2(false))
.ContinueWith(t => t.IsFaulted ? throw t.Exception!.Flatten() : t);
catch (AggregateException ex)
Console.WriteLine(string.Join(",\n", ex.InnerExceptions.Select(x => x.Message)));
Console.WriteLine("TEST SUCCESS UNEXPECTED EXCEPTION: " + ex.GetType().FullName);
Console.WriteLine("TEST SUCCESS: OK");
private static async Task TestFail()
.WhenAll(DoLongThingAsyncEx1(true), DoLongThingAsyncEx2(true))
.ContinueWith(t => t.IsFaulted ? throw t.Exception!.Flatten() : t);
catch (AggregateException ex)
Console.WriteLine(string.Join(",\n", ex.InnerExceptions.Select(x => x.Message)));
Console.WriteLine("TEST FAIL UNEXPECTED EXCEPTION: " + ex.GetType().FullName);
Console.WriteLine("TEST FAIL: OK");
private static async Task TestCanceled()
.WhenAll(DoLongThingAsyncEx1(false), DoLongThingAsyncEx2(false))
.ContinueWith(t => throw t.Exception!.Flatten(), TaskContinuationOptions.OnlyOnFaulted);
catch (AggregateException ex)
Console.WriteLine(string.Join(",\n", ex.InnerExceptions.Select(x => x.Message)));
Console.WriteLine("TEST CANCELED UNEXPECTED EXCEPTION: " + ex.GetType().FullName);
Console.WriteLine("TEST CANCELED: NOK");
private static Task DoLongThingAsyncEx1(bool error)
if (error) return Task.Run(() => { throw new InvalidTimeZoneException(); });
return Task.FromResult<bool>(true);
private static Task DoLongThingAsyncEx2(bool error)
if (error) return Task.Run(() => { throw new InvalidOperationException();});
return Task.FromResult<bool>(true);