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 TryEnd Sub

As you can see, there is no difference in handling exceptions inside asynchronous methods compared to classic ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.