Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Created March 6, 2019 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-williams/cfea73dd84b53bdb3647a582068ff77e to your computer and use it in GitHub Desktop.
Save andy-williams/cfea73dd84b53bdb3647a582068ff77e to your computer and use it in GitHub Desktop.
Get exceptions of all tasks with Task.WhenAll
async Task Main()
{
var task1 = Task.Run(async () => {
await Task.Delay(500);
throw new Exception("Exception from task 1");
});
var task2 = Task.Run(() =>
{
throw new Exception("Exception from task 2");
});
var tasks = new Task[] { task1, task2 };
try
{
await Task.WhenAll(tasks);
}
catch(Exception)
{
var exceptions = tasks.Where(t => t.Exception != null)
.Select(t => t.Exception);
exceptions.Dump();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment