Junior — Senior
Exceptions in asynchronous calls: where are they caught?
livecode
Task condition
Determine which catch blocks will catch the thrown exception and what lines will appear in the console output.
using static System.Console;
async Task RunAsync() => throw new NotImplementedException();
Task myTask = Task.CompletedTask;
try
{
myTask = RunAsync();
}
catch (Exception caught)
{
Console.WriteLine($"M0: {caught.GetType().Name}");
}
try
{
await myTask;
}
catch (Exception caught)
{
Console.WriteLine($"M1: {caught.GetType().Name}");
}
try
{
myTask.Wait();
}
catch (Exception caught)
{
Console.WriteLine($"M2: {caught.GetType().Name}");
}