Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
4 Jan 2015 1  
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 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here