I have a hard time understanding how async/await works in various non-happy-path cases. For example, I have the following code:
class Program
{
static void Main(string[] args)
{
Do();
Console.ReadLine();
}
private static void Do()
{
TaskScheduler.UnobservedTaskException = (s, e) =>
{
Console.WriteLine($"Unobserved Exception : {e.Exception.Message}");
e.SetObserved();
};
try
{
ThrowsAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Caught in try/catch : {ex.Message}");
}
}
private static async Task ThrowsAsync()
{
Console.WriteLine("Throwing");
throw new Exception("FAILURE");
}
}
There are two things that I do not understand:
- The
ThrowsAsync
method is async, however, it does not contain anyawait
. I would assume that in such a case the method would execute like a "normal" synchronous method. However, the exception that it throws is never caught in thecatch
block. - Trying to somehow catch the exception, I added the handler for
TaskScheduler.UnobservedTaskException
. However, it is never executed. Why is that?
I know that the exception would be caught if I awaited ThrowsAsync
. However, I'm experimenting to get a better understanding of how it works.
I'm running that code using .NET 5 and Linux-based OS.
CodePudding user response:
As described for example in this blog post by Stephen Cleary - the state machine for
async
methods will capture exceptions from your code and place them on the returned task, i.e. method invocation will not throw, you will be able to catch exception ifawait
the result.As for
TaskScheduler.UnobservedTaskException
- check out this answer and be sure to run code inRelease
mode.