C# 6.0 Exception Filters. try catch when

Source: http://druss.co/2015/05/c-6-0-exception-filters-try-catch-when/

Exception filters is a new C# 6.0 feature. Visual Basic.NET and F# have this functionality for a long time. That is because exception filtering was implemented in CIL but not in C#. Now, this technique available for us. That’s how you can use it:

try
{
    Method();
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 0x07)
{
    // do exception handling logic
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 0x148)
{
    // do exception handling logic
}
catch (Exception)
{
    // log unhandled exception and rethrow
    throw;
}

If Method throws an exception, catch block will first try to match type of the exception and then will evaluatewhen block. If an expression in when block is true, execution will go to the catch block. If not, the next catch block will be evaluated.