`async void` Anti-Pattern & Global Crash Traps
1Concept
`async void` methods cannot be awaited and unhandled exceptions cannot be caught by surrounding `try-catch` blocks, causing the entire process to crash immediately.
2Architecture Diagram
async Task Method(): ──> Caller can `await` and catch exceptions inside `try-catch` async void Method(): ──> Exceptions bypass catch blocks and crash CLR process!
3Code Example
C# 13 & .NET 9
using System;
using System.Threading.Tasks;
public class AsyncVoidTrapDemo
{
// Good: Returns Task
public static async Task SafeAsyncMethod()
{
await Task.Delay(5);
Console.WriteLine("Safe async task completed.");
}
public static async Task Main()
{
await SafeAsyncMethod();
Console.WriteLine("Only top-level UI event handlers (e.g. Button_Click) should use async void.");
}
}4Expected Output
Safe async task completed. Only top-level UI event handlers (e.g. Button_Click) should use async void.
5Key Takeaways
- ✓Never use `async void` except in top-level UI event handlers.
- ✓Always return `Task` or `ValueTask` for async methods.
- ✓Async void exceptions trigger `AppDomain.UnhandledException` directly.