Skip to content

Exceptions cannot be caught

Context: Exceptions thrown from an async void method cannot be caught by the caller; they crash the process or cause the application to terminate.

using System;
using System.Threading.Tasks;
public class BadExample
{
public static void Main()
{
try
{
BadAsyncVoid();
}
catch (Exception)
{
Console.WriteLine("This will never execute");
}
Console.ReadLine(); // process may crash before this
}
static async void BadAsyncVoid()
{
throw new InvalidOperationException("Crash");
}
}

Return Task instead of void.

async Task GoodAsync()
{
throw new InvalidOperationException("Will be caught");
}

Avoid in library code: Never expose async void in public API. Only use async void for UI event handlers where the framework handles exceptions.

Example: Async guidelines - avoid async void