Created
March 6, 2019 13:56
-
-
Save andy-williams/cfea73dd84b53bdb3647a582068ff77e to your computer and use it in GitHub Desktop.
Get exceptions of all tasks with Task.WhenAll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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