Skip to content
Tutorials Center
My learning

Topics

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.

C#
try
{
    await LoadAsync();
}
catch (HttpRequestException ex)
{
    Console.WriteLine(ex.Message);
}

Try it

C# Await, Wait and AggregateException

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.

C# Several failures with WhenAll

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

C#
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.UnobservedTaskException when the task is collected.
  • Catch the right layer. Put try/catch around the await, not around the call that returns the task. This catches nothing: try { var t = DoAsync(); } catch { }.
  • Avoid async void except for event handlers. Callers cannot await it or catch its exceptions. Use async Task and test it.
  • Validate arguments eagerly. In an async method, ArgumentNullException is stored in the task and appears at the await. A non-async method that checks arguments and then calls a private async method throws at the call.
  • Cancellation is not failure. Catch OperationCanceledException separately (or use when (ex is not OperationCanceledException)) so cancellation does not get logged as an error.
  • AggregateException.Flatten() turns nested aggregates into a flat list; Handle lets you process the exceptions you recognize and rethrows the others.
  • Fire-and-forget safely: wrap the work in a method with its own try/catch that 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;; use throw; to keep the original stack trace.