Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
This chapter is from the book

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.

  • + Share This
  • 🔖 Save To Your Account