␡
- Overview of Asynchrony
- The Old-Fashioned Way: Event-Based Asynchrony
- The Old-Fashioned Way: The Asynchronous Programming Model
- The Modern Way: The Async Pattern
- Getting Started with Async/ Await
- Exception Handling in Async
- Implementing Task-Based Asynchrony
- Cancellation and Progress
- Asynchronous Lambda Expressions
- Asynchronous I/O File Operations in .NET 4.6
- Debugging Tasks
- Summary
This chapter is from the book
Exception Handling in Async
Another great benefit of using the Async pattern is that exception handling is done the usual way. In fact, if an awaited method throws an exception, this can be naturally handled within a Try..Catch..Finally block. The following code provides an example:
Private Async Sub DownloadSomethingAsync() Dim client As New System.Net.WebClient Try Dim result = Await client. DownloadStringTaskAsync("http://msdn.com/vbasic") Catch ex As Exception Console.WriteLine(ex.Message) Finally Console.WriteLine("Operation completed.") End Try End Sub
As you can see, there is no difference in handling exceptions inside asynchronous methods compared to classic synchronous methods. This makes code migration easier.