Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

What’s New in C# 6.0? - await in catch and finally Blocks

Rate me:
Please Sign up or sign in to vote.
4.70/5 (3 votes)
4 Jan 2015CPOL2 min read 348.5K   6   1
What’s new in C# 6.0? - await in catch and finally blocks

Image 1

C# 6.0 came with another new feature along with Visual Studio 2015 and .NET 4.6. Now you will be able to write await operations in catch {} and finally {} blocks too. This will help many developers to do certain async operations when they encounter any exceptions or want to do async clean-ups in finally block.

Today in this blog post, we will discuss about this new feature. Don’t forget to read the other new features which I posted earlier. Links can be found in this post.

Whats new in CSharp 6.0 - header

Don’t forget to read my previous posts on this series:

Prior to C# 6.0, it was not possible to call async operations in catch {} and finally {} blocks by specifying the await keyword due to implementation specific issues in the C# compiler. But now, Microsoft engineers made this impossible task possible by changing their codes in the compiler part. Now you will be able to call the await statements in catch {} and finally {} blocks without writing any complicated code. The compiler will automatically handle it.

Here is a code snippet to show you how easily you can call the await for async operations in catch {} and finally {} blocks. Most of the time, we need this to log exceptions and/or do some final clean-ups of the resources.

Whats new in CSharp 6.0 - await in catch and finally blocks

This feature was in the list of much demanded features by the developers as they had to write some complicated logic in order to implement the same in their code as a workaround. This was due to C# compiler limitations in those days. But now, if you are using C# 6.0, this limitation is gone. Use it whenever you need.

How much did you like the feature and how much will you use it in coming projects when you start using C# 6.0? Do let us know in the comment section below.

Don’t forget to subscribe to my blog’s RSS feed and Email Newsletter to get immediate updates directly delivered to your inbox. I am available on Twitter, Facebook, Google+ and LinkedIn. Do connect with me there and get the updates that I share over those social networking sites.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 3 Pin
fraser addison5-Jan-15 7:13
fraser addison5-Jan-15 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.