What’s new in C# 6.0? - await in catch and finally blocks


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.


 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

3 comments

  1. This is nice. Wish we would've gotten awaitable properties as well.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top