Back to Devexpress

CRR0033 - The void async method should be in a try/catch block

coderushforroslyn-119689-static-code-analysis-analyzers-library-crr0033-the-void-async-method-should-be-in-a-try-catch-block.md

latest1.4 KB
Original Source

CRR0033 - The void async method should be in a try/catch block

  • Feb 28, 2025

This analyzer detects asynchronous methods that return nothing and do not catch the inner exceptions.

csharp
async void DemoMethodAsync(CancellationToken token) {
    await Task.Run(() => { DemoMethodSync(); }, token).ConfigureAwait(false);
}
vb
Async Sub DemoMethodAsync(token As CancellationToken)
    Await Task.Run(Sub() DemoMethodSync(), token).ConfigureAwait(False)
End Sub

The code above can cause the application to crash because the exceptions that are thrown inside an asynchronous void method cannot be caught from the outside. You should catch the exceptions inside the method.

csharp
async void DemoMethodAsync(CancellationToken token) {
    try {
        await Task.Run(() => { DemoMethodSync(); }, token).ConfigureAwait(false);
    } catch(Exception ex) {
        Console.WriteLine(ex);
    }
}
vb
Async Sub DemoMethodAsync(token As CancellationToken)
    Try
        Await Task.Run(Sub() DemoMethodSync(), token).ConfigureAwait(False)
    Catch ex As Exception
        Console.WriteLine(ex)
    End Try
End Sub

See Also

Suppress Analyzers