C# 6 Features – “Await in catch and finally blocks.”

The C# team had recently announced a few new features for C# 6 that will ships with VS 2015. One of the feature that caught my attention is “Await in catch and finally blocks.” Recall that I actually had a post on “await MessageDialog in the body of a catch clause” in which the current version of C# does not allow await in catch statement thus leading to ugly workaround in order to still use a function that requires await.

All this is set to change in C# 6 as shown below.


Resource res = null; 
try 
{ 
    res = await Resource.OpenAsync(…); // You could always do this. 
    … 
} 
catch(ResourceException e) 
{ 

    await Resource.LogAsync(res, e); // Now you can do this … 
} 
finally 
{ 
    if (res != null) await res.CloseAsync(); // … and this. 
}

For more on what is new in C# 6, click here

Advertisements
This entry was posted in C# and tagged , . Bookmark the permalink.

One Response to C# 6 Features – “Await in catch and finally blocks.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s