chapter twelve

12 Exceptions and async-await

 

This chapter covers

  • How exceptions work with asynchronous code
  • Dealing with lost exceptions
  • Handling exceptions in async void methods

In this chapter we are going to talk about exceptions. We’re going to discuss how exceptions work in asynchronous code and how that differs from how exceptions work in traditional non-asynchronous code. In transitional code exceptions bubble up the call stack, in asynchronous code (like we’ve seen in chapters 3, 5 and 11) you constantly register callbacks to be called later, often from other threads, and therefore the call stack no longer describes the flow of your code. This knowledge, and knowledge about what async-await does to mitigate this, is important to debug issues related to exceptions in asynchronous code, that is, to debug any situation where the asynchronous code fails in a non-straightforward way. We’ll also cover some pitfalls related to exceptions you should be aware of.

12.1 Exceptions and asynchronous code

12.2 await and AggregateException

12.3 The case of the lost exception

12.4 Exceptions and async void methods

12.5 Summary