Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C# 6.0

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (8 votes)
13 Dec 2014CPOL2 min read 34.6K   7   5
Exception filters in C# 6.0

Image 1

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.

Image 2

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 5 Pin
Thomas Maierhofer (Tom)15-Dec-14 2:59
Thomas Maierhofer (Tom)15-Dec-14 2:59 
GeneralRe: My vote of 5 Pin
James Lonero16-Dec-14 17:17
James Lonero16-Dec-14 17:17 
GeneralMy vote of 4 Pin
K. Naveen. Bhat14-Dec-14 22:33
K. Naveen. Bhat14-Dec-14 22:33 
GeneralRe: My vote of 4 Pin
corleymanz15-Dec-14 0:40
corleymanz15-Dec-14 0:40 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»15-Dec-14 6:13
professionalKunal Chowdhury «IN»15-Dec-14 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.