C# Reference ยท Async and Concurrency
Exceptions in async code
How exceptions behave in async C#: stored in tasks, rethrown at await, AggregateException with Wait and Result, WhenAll failures, async void and unobserved tasks.
An exception thrown inside an async method returning Task or Task<T> is not thrown at the call. The compiler catches it and stores it in the returned task, which becomes faulted. The exception is raised again where you await that task, so a normal try/catch around the await works as you would expect.
The details change when you block on a task, combine several tasks, or use async void. This page covers each case.
try { await LoadAsync(); } catch (HttpRequestException ex) { Console.WriteLine(ex.Message); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
try { await operation; } catch (SpecificException ex) { ... } catch (OperationCanceledException) { ... } // before a general catch finally { ... } // await is allowed in catch and finally Task t = ...; AggregateException? all = t.Exception; // every stored exception t.Wait(); // throws AggregateException t.GetAwaiter().GetResult(); // throws the original exception, blocking ExceptionDispatchInfo.Capture(ex).Throw(); // rethrow later keeping the stack trace
| Situation | What you see |
|---|---|
await a faulted task |
The first inner exception, with its original type and stack. |
Wait(), Result, WaitAll |
An AggregateException that wraps the inner exceptions. |
await Task.WhenAll(...) |
The first exception. The rest stay on the combined task. |
| Canceled task | TaskCanceledException (an OperationCanceledException). |
async void |
The exception is raised on the SynchronizationContext, or on the thread pool when there is none, which normally ends the process. |
Errors: the exception types depend on your code; the point is where each type is raised.
Usage notes
- Await every task. A faulted task that nobody awaits fails silently. Since .NET 4.5 an unobserved task exception no longer terminates the process; it raises
TaskScheduler.UnobservedTaskExceptionwhen the task is collected. - Catch the right layer. Put
try/catcharound theawait, not around the call that returns the task. This catches nothing:try { var t = DoAsync(); } catch { }. - Avoid
async voidexcept for event handlers. Callers cannot await it or catch its exceptions. Useasync Taskand test it. - Validate arguments eagerly. In an
asyncmethod,ArgumentNullExceptionis stored in the task and appears at theawait. A non-async method that checks arguments and then calls a private async method throws at the call. - Cancellation is not failure. Catch
OperationCanceledExceptionseparately (or usewhen (ex is not OperationCanceledException)) so cancellation does not get logged as an error. AggregateException.Flatten()turns nested aggregates into a flat list;Handlelets you process the exceptions you recognize and rethrows the others.- Fire-and-forget safely: wrap the work in a method with its own
try/catchthat logs, and then discard the task deliberately with_ =. - Stack traces from awaited exceptions show the logical async call chain, with
--- End of stack trace from previous location ---markers. - Do not rethrow with
throw ex;; usethrow;to keep the original stack trace.