Code inspection: Do not rethrow exception in 'async void' methods and functions
This inspection identifies cases where exceptions are rethrown in async void methods or functions, potentially leading to application crashes. Unlike async Task or async Task<T> methods, exceptions in async void methods are not awaited directly and propagate to the synchronization context or unhandled exception handler. This behavior can cause the entire application process to terminate.
class Example
{
async void FaultyMethod()
{
try
{
await Task.Delay(100);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw; // Warning: Can crash the process
}
}
}
To avoid runtime crashes, replace the throw; statement with a proper exception handling.
If possible, also consider using Task or Task<T> as the return type of async methods to ensure better exception handling and maintainability.
16 July 2025