Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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