Subscribe to our YouTube Channel for more Videos

Await in Catch and finally block in C# 6.0


C# 6.0 Features Series

In the earlier versions of C# , there was no provision to have the await keyword in the catch and finally block . This was a kind of limitation and developers had to use a workaround to get this to work.

In C# 6.0 , the await can now be used within the the catch and finally block.

Await in Catch and finally block in C# 6.0

WebClient client = null;
try
{
    client = new WebClient();
    string response = await client.DownloadStringTaskAsync(new Uri("http://developerpublish.com"));
    Console.WriteLine(response);
}
catch (Exception e)
{
    // Logging Logic
    await Logger.LogAsync(client, e);
}
finally
{

}
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.