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 below comment section.

 

Don’t forget to subscribe to my blog’s RSS feed and Email Newsletter to get the 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.


 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.