#1,219 – C# 6.0 – Filtering Exceptions
November 5, 2014 13 Comments
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); }
Pingback: Dew Drop – November 5, 2014 (#1892) | Morning Dew
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.
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.
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#.
@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.
Mourad – you can re-throw the exception if the expression is false.
@Steven – …which is not nearly as clean or efficient as not catching the exception in the first place.
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);
}
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.
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!!!
Anyone has a pdf version of all the posts?
What happened with this blog after this last post? You promised us 2000 things!
Life happened.