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

What’s New in C# 6.0? - Exception Filters

0.00/5 (No votes)
13 Dec 2014 3  
Exception filters in C# 6.0

C# 6.0 brought another great new feature named “Exception Filters” along with Visual Studio 2015 and .NET 4.6. If you didn’t try yet the preview version of the new IDE, go and grab it to get your hands dirty with the new features before it actually releases.

Today in this post, we will discuss about the new feature “Exception Filters”. Read more to learn about it. Don’t forget to share the feature links in your network.

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

Exception filters” is a CLR capability which is already present in Visual Basic and F#, but was not present in C#. In C# 6.0, Microsoft included this feature for CSharp developers to use in their code. If you want to use Exception Filter, you have to declare it in the same line where you declared the catch block, just like this: catch (Exception ex) if (FilterIsTrue). Here, if the parenthesized expression after the “if” returns true, the associated catch block will execute. Otherwise, it will move forward.

Expression Filters can be used in many ways. They are preferable to catch and re-throw because they keep the stack intact. If the exception later causes the stack to dump, you can see the original source of it, rather than just the last place where it was re-thrown. Isn’t it good?

Check out below to know, how you can write the Expression Filters in catch statement of any try {} catch {} block. This is just a sample to help you understand it better, but you can use it in a different way.

Whats new in CSharp 6.0 - Exception Filters

Here, you can actually see how it executes when the said line calls and how the debugger actually passes to the next if statement of the catch in case the if statement evaluates false. Not only can you check for conditions, but you can also call some methods to log the details of the entire stack trace.

Whats new in CSharp 6.0 - Exception Filters

So, what’s your opinion about this feature? How will you use it in your project? Don’t forget to share your comments below. And yes, if you have any feedback on my post, do not forget to let me know.

I am available on Twitter, Facebook, Google+. Do connect with me to subscribe to my feed to get the updates on what I share over those social networks. Subscribe to my blog’s RSS feed and Email Newsletter to receive the new article publish notification in your inbox.

Reference: http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or you may like the Facebook page of my blog http://www.facebook.com/blog.kunal.

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