How it works
An async operation can complete in a faulted state. Await observes that completion and throws the failure into the caller’s normal exception handling flow. The catch here handles a specific InvalidOperationException and prints a stable message. It does not pretend the operation succeeded or substitute a plausible number. Keep the catch close to the boundary that can make a useful decision, such as showing a retry action or rejecting an import.
A broad catch that silently ignores every exception can turn programming errors into incorrect application state. Return Task rather than async void so callers can await completion and observe errors.
Replace the thrown exception with a returned value and observe the successful path. Then change the exception type and explain why a specific catch deliberately does not handle every possible failure.
A working example
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
try { Console.WriteLine(await ReadAsync()); }
catch (InvalidOperationException) { Console.WriteLine("read failed"); }
async Task<int> ReadAsync()
{
await Task.Delay(1);
throw new InvalidOperationException("Missing sample data");
}
Expected output
read failed
Handle failures at a boundary that can decide what to do.