How to Handle Exceptions in Async C# Correctly
try-catch blocks around await calls. This ensures exceptions thrown inside async tasks are caught properly and do not crash your program.Why This Happens
When you call an async method without await or without proper try-catch, exceptions inside the async task are not caught where you expect. This causes unhandled exceptions or crashes because the error happens on a different thread context.
using System; using System.Threading.Tasks; class Program { static async Task ThrowExceptionAsync() { await Task.Delay(100); throw new InvalidOperationException("Something went wrong!"); } static void Main() { try { // Calling async method without await ThrowExceptionAsync(); } catch (Exception ex) { Console.WriteLine($"Caught exception: {ex.Message}"); } Console.WriteLine("Main method finished."); Console.ReadLine(); } }
The Fix
Use await when calling async methods inside a try-catch block. This makes sure exceptions thrown inside the async method are caught in the catch block, preventing crashes and allowing graceful error handling.
using System; using System.Threading.Tasks; class Program { static async Task ThrowExceptionAsync() { await Task.Delay(100); throw new InvalidOperationException("Something went wrong!"); } static async Task Main() { try { await ThrowExceptionAsync(); } catch (Exception ex) { Console.WriteLine($"Caught exception: {ex.Message}"); } Console.WriteLine("Main method finished."); } }
Prevention
Always await async methods to catch exceptions properly. Use try-catch blocks around await calls to handle errors gracefully. Avoid calling async methods without await unless you explicitly handle the returned Task. Consider using tools like analyzers or linters that warn about unawaited tasks to prevent silent failures.
Related Errors
Common related errors include:
- UnobservedTaskException: Happens when a faulted task is not awaited or observed.
- Deadlocks: Blocking on async code with
.Resultor.Wait()can cause deadlocks. - Swallowed exceptions: Forgetting to await tasks can hide exceptions.