Exception Filters in C-Sharp 6.0

So far we deal with the Exceptions as below. As you see in the following code snippet, we are catching the exception and displaying the error message.


using System;
namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = 24;
            try
            {

                int rem = number % 0;
                Console.WriteLine(rem);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey(true);
        }
    }
}


Challenge in the above snippet is that you cannot apply filter to the exception. Of course you can have if-else statement inside the try catch block but it will cause a particular catch block to get executed and then filter would get applied.

In C# 6.0 a new feature got introduced to apply filter at the catch block. This feature is known as Exception filters. Now a particular catch block can be executed only of the filter is set to true.

image

In the above snippet we are filtering the exception on the various basis like if message contains a particular string then execute a particular catch block or on the basis of the exception source execute a particular catch block.

This feature may be useful in the scenarios in which, you need to perform various actions on if a condition is met in a particular exception type.

For your reference the source code is given below,

using System;
namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = 24;
            try
            {

                int rem = number % 0;
                Console.WriteLine(rem);
            }
            catch (DivideByZeroException ex) if (ex.Message.Contains("dj"))
            {
                Console.WriteLine(ex.Message);
            }
            catch(DivideByZeroException ex) if (ex.Source == "demo1")
            {
                Console.WriteLine(ex.Source);
            }

            Console.ReadKey(true);
        }
    }
}


Exception message does not contain any string dj hence the first catch block will not get executed. However second catch block will be executed since source of the exception is demo1. You will get the expected output as below:

image

I hope concept of Exception filter is clear to you now and you will harness C# 6.0 feature in your project.

Happy coding

3 responses to “Exception Filters in C-Sharp 6.0”

  1. […] Exception Filters in C-Sharp 6.0 and Property Initializers in C-Sharp 6.0 (Dhananjay Kumar) […]

  2. What will get possible with the new syntax which wasn’t possible before? It’s not good language design when there are multiple syntax styles to do the same. Also this could promote the use of the anti-pattern “exceptions as control flow” if a control flow statement gets a primary feature of the catch block.

  3. There is nothing great in this syntax. Just adding an if condition along with the catch statement. This is similar to the below:

    catch (DivideByZeroException ex)
    {
    if (ex.Message.Contains(“dj”))
    {
    Console.WriteLine(ex.Message);
    }
    }

    So, I couldn’t understand what’s the advantage here.

Leave a comment

Create a website or blog at WordPress.com