#1,219 – C# 6.0 – Filtering Exceptions

C# 6.0 will include support for exception filters, that is–only catching a particular type of exception if an associated expression evaluates to true.

You can filter exceptions by including an if statement after the catch expression.  If the result of evaluating the expression supplied is true, the exception is caught.  If not, the behavior is as if you didn’t supply a catch block.

In the example below, we don’t catch divide by zero exceptions on Saturdays.

            int denom;
            try
            {
                denom = 0;
                int x = 5 / denom;
            }
            // Catch /0 on all days but Saturday
            catch (DivideByZeroException xx) if (DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
            {
                Console.WriteLine(xx);
            }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

6 Responses to #1,219 – C# 6.0 – Filtering Exceptions

  1. Pingback: Dew Drop – November 5, 2014 (#1892) | Morning Dew

  2. Kevin Marois says:

    When I saw the title “Filtering Exceptions” I thought “Interesting” – but you can already simply put an IF block inside the CATCH. Same amount of code that does the same thing.

  3. Vincent says:

    From https://msdn.microsoft.com/en-us/magazine/dn683793.aspx :

    Note that unlike some of the other C# 6.0 features discussed earlier […], there was no equivalent alternate way of coding exception filters prior to C# 6.0. Until now, the only approach was to catch all exceptions of a particular type, explicitly check the exception context, and then re-throw the exception if the current state wasn’t a valid exception-catching scenario. In other words, exception filtering in C# 6.0 provides functionality that hitherto wasn’t equivalently possible in C#.

  4. @Kevin: they are not the same:
    If the condition is inside the catch block, you are technically catching the exception; subsequent catch statements are not checked. While with Exception Filters, the condition determines whether the exception should be caught and handled by this catch block; if false, then the exception is not caught, the catch block is skipped and the code moves to the next catch block.

  5. Steven says:

    Mourad – you can re-throw the exception if the expression is false.

    • Gregg says:

      @Steven – …which is not nearly as clean or efficient as not catching the exception in the first place.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: