Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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

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... - Article authored by Kunal Chowdhury on .

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 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 you can check for conditions, but also can 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.


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.