Exception Filtering: A New Feature of C# 6.0

Introduction 

On the day of Visual Studio Connect() event held on November 12, 2014 Microsoft introduced Visual Studio 2015 Preview with many new and exciting features for developers including the open source community and .NET 4.6 preview that are released for testing purposes. You can easily download the Visual Studio Preview 2015 from here

    So far, this is the first preview version of Visual Studio 2015 available to download and we hope it will become better in the future. So don't wait to try out this new release and help Microsoft to provide us the final release of the Visual Studio 2015 IDE as soon as possible and don't forget to submit your valuable feedback to Microsoft about your experience with Visual Studio 2015 Preview.

    Don't forget to read my previous posts on this series: "A new feature of C# 6.0"

    Getting to C#, C# 6.0 brought a set of very useful features with Visual Studio 2015 Preview and .NET 4.6. Let's have a look at one of the features, Exception Filter.
     
    Exception Filtering

    An Exception Filter is a feature already present in Visual Basic and F#. Microsoft now introduced this feature in C# 6.0 for developers to use in their code. Exception filters allow you to specify a conditional clause for each catch block. To use exception filters we need to declare them in the same line as where the catch block is declared, just like this: catch ( Exception e) if(filtercondition). Instead of a catch block handling all exceptions of a specific type, they allow us to write catch blocks that handle when a certain condition is true. If the parenthesized expression after the "if" condition evaluates to true, the catch block will be executed, else the exception continues. Catching and rethrowing are preferred using exception filters because they leave the stack unharmed and if the exception later causes the stack to be dumped, we can see where it originated from.

    Check out the following code snippet to understand how to write the Expression Filters in a catch statement of any try {} catch {} block. 

    1. try  
    2. {  
    3.    int number = 0;  
    4.    int x = 5 / number;  
    5. }  
    6. catch (DivideByZeroException) if (DateTime.Now.DayOfWeek != DayOfWeek.Tuesday)  
    7. {  
    8.    Console.WriteLine("Sorry, today is not Tuesday");  
    9. }  
    10. catch (DivideByZeroException) if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)  
    11. {  
    12.    Console.WriteLine("Hurray It's Tuesday");  
    13. }  
    In the preceding code snippet, the try block is generating the exception DivideByZeroException and we have defined two DivideByZeroException catch blocks with different conditions. In the first condition we are checking if the present day is not Tuesday then the catch block will be executed and "Sorry, today is not Tuesday" will be printed. And in the second DivideByZeroException catch block we are checking if today's day is Tuesday and if so then the second catch block will be executed and "Hurray It's Tuesday" will be printed. 

    Demo Application using Visual Studio 2013 

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6.   
    7. namespace CSharpFeatures  
    8. {  
    9.     class ExceptionFilters  
    10.     {  
    11.         static void Main(String[] args)         //using Visual Studio 2013  
    12.         {  
    13.             try  
    14.             {  
    15.                 int number = 0;  
    16.                 int x = 5 / number;  
    17.             }  
    18.             catch (DivideByZeroException)  
    19.             {  
    20.                 if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)  
    21.                 {  
    22.                     Console.WriteLine("Yes it is Tuesday");  
    23.                 }  
    24.                 else  
    25.                 {  
    26.                     Console.WriteLine("No its not tuesday");  
    27.                 }  
    28.             }  
    29.             Console.WriteLine("Press any key to exit..");  
    30.             Console.Read();  
    31.         }  
    32.     }  
    33. }  

    Demo Application using Visual Studio 2015 Preview

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6.   
    7. namespace CSharpFeatures  
    8. {  
    9.     class ExceptionFilters  
    10.     {  
    11.         static void Main(String[] args)         //using Visual Studio 2015  
    12.         {  
    13.             try  
    14.             {  
    15.                 int number = 0;  
    16.                 int x = 5 / number;  
    17.             }  
    18.             catch (DivideByZeroException) if (DateTime.Now.DayOfWeek != DayOfWeek.Tuesday)  
    19.             {  
    20.                 Console.WriteLine("Sorry, today is not Tuesday");  
    21.             }  
    22.             catch (DivideByZeroException) if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)  
    23.             {  
    24.                 Console.WriteLine("Hurray It's Tuesday");  
    25.             }  
    26.             Console.WriteLine("Press any key to exit");  
    27.             Console.Read();  
    28.         }  
    29.     }  
    30. }  

    Summary

    In this article we learned how to use exception filtering in catch statements of any try {} catch {} block. That means that without catching the exception we are checking for a condition and if the condition is not met, the next catch block is considered.

    So, what's your opinion about this feature? How will you use it in your project? Your comments are most welcome.