Handling Async Await Exceptions

Async Await Exceptions

Async/Await

There’s a ton of information about async await support in .NET 4.5.  I don’t want to go over all the best practices and gotchas in this post, but I did want to point out one common trend I’ve seen lately, which is handling async await exceptions.

Await or forget about it

Any async method that returns a Task or Task<T>, you should always await the result.

If you do not await, any exceptions that occur in the calling method will be swallowed.  Essentially you are turning the method call into a fire and forget.

Async Await Exceptions Example

Below is a simple example that demonstrates two behaviors of calling await on an async.  

To run the example, just copy and paste in a new console application.  If you run with the debugger, you should notice the following:

  1. MethodCallWithoutAwait will be called, however no exception will be raised in the debugger because await is not called.
  2. MethodCallWithAwait will be called and the exception will be raised in the debugger because await is called.

Async Lambdas

There be dragons!  Next post I’ll explore of using async lambdas and how you need to pay close attention in order to avoid creating a void lambda.