Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
Async/AwaitThere’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 itAny 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 ExampleBelow 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:
- MethodCallWithoutAwait will be called, however no exception will be raised in the debugger because await is not called.
- MethodCallWithAwait will be called and the exception will be raised in the debugger because await is called.