VisualStudioFeeds

All Visual Studio blogs in one place

AddThis Social Bookmark Button
In the previous blog post, we looked at the basics of async. In this post, we will look at exception handling. You need exception handling the moment something goes wrong in an asynchronous call. When this happens, the Task that comes out of the asynchronous call will have an error status and you won't be able to get the result from the method.

Handling these kinds of error situations is actually really simple. The only thing you need to do is wrap your asynchronous call in a try - catch statement, just like you would do with synchronous
code. Let's take a look at an example:

public async Task PublishHikeRequest()
{
var hikeRequest = GetHikeRequest();

try
{
var client = new HttpClient();

var url = string.Format("{0}/HikeRequest", BASE_URL);
var response = await client.PostAsync(url, await BuildJsonContent(hikeRequest));
response.EnsureSuccessStatusCode();
}
catch (Exception exc)
...(Read whole news on source site)

Search

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : ProQ. Gitte Vermeiren : Async II: Exception Handling