C#C
C#3y ago
qqdev

❔ This is not a .NET bug, right? (async void)

Hi! Is this by design?
using System;
using System.Threading.Tasks;

public class Program
{
    public static void Main()
    {
        DoStuff();
    }

    private static async void DoStuff()
    {
        try
        {
            await DoAsyncStuff();
        }
        catch
        {
            Console.WriteLine("catch");
        }
        finally
        {
            Console.WriteLine("finally");
        }
    }

    private static async Task DoAsyncStuff()
    {
        await Task.Delay(1);
        throw new Exception();
    }
}


Neither catch nor finally is getting hit. I know that exceptions which are being raised in async void methods are being swallowed but didn't think that those in a try block would get swallowed aswell.

For reference:
https://stackoverflow.com/a/5383408
https://stackoverflow.com/a/16158374

^ Those answers state that the exceptions are getting catched
Was this page helpful?