#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 a when 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) when (DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
            {
                Console.WriteLine(xx);
            }
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

13 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.

    • Chris says:

      Not true. If you rely on the If block your exception is already caught. If you filter, your exception is uncaught if it misses the filter. You’d need to deal with rethrowing your exception in your If block if you want to “skip” the catch behavior.

  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.

  6. breen says:

    Did you intend to use the when statement in your sample, as this is the keyword used for C# 6 exception filtering:
    int denom;
    try
    {
    denom = 0;
    int x = 5 / denom;
    }
    // Catch /0 on all days but Saturday
    catch (DivideByZeroException xx) when (DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
    {
    Console.WriteLine(xx);
    }

    • Sean says:

      No, I did intend to use the if statement. This post was written based on early documentation of syntax, so the if statement was used. In the final release, I believe that the syntax changed to use the when keyword.

  7. John says:

    This is a great tip. There plenty of reasons to do one thing for one exception, and a different thing for a different exception. Awesome!!!

  8. Ravi says:

    Anyone has a pdf version of all the posts?

  9. Thomas says:

    What happened with this blog after this last post? You promised us 2000 things!

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: