Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created January 17, 2022 00:21
Show Gist options
  • Select an option

  • Save xiaomi7732/2544c9b8cb9a0d4d76a80425e5b04a4d to your computer and use it in GitHub Desktop.

Select an option

Save xiaomi7732/2544c9b8cb9a0d4d76a80425e5b04a4d to your computer and use it in GitHub Desktop.
Why async void is evil
async void AsyncJob()
{
await Task.Delay(100);
throw new InvalidOperationException("Hello exception!");
// Console.WriteLine("Job is done!");
}
try
{
// Notice: the exception thrown by AsyncJob won't be
// caught by this try catch.
// It will crash the process instead.
AsyncJob();
}
catch (Exception)
{
Console.WriteLine("Friendly error.");
}
Console.WriteLine("Hello from main!");
Console.ReadKey(intercept: true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment