C# - 6.0 Exception Filters

Posted on 2015-08-01

Before C# 6

static void Main ( string [ ] args )  
{ 
    try
    {
        doSomething();
    }
    catch (Exception ex) 
    {
        if (ex.Message == "DataNotFount")
        {
            Console.WriteLine("DataNotFount");
        }
        else if (ex.Message == "ChangePasswordError")
        {
            Console.WriteLine("ChangePasswordError");
        }
    }
} 

C# 6

static void Main ( string [ ] args )  
{ 
    try
    {
        doSomething();
    }
    catch (Exception ex) when (ex.Message == "DataNotFount")
    {
        Console.WriteLine("DataNotFount");
    }
    catch (Exception ex) when (ex.Message == "ChangePasswordError")
    {
        Console.WriteLine("ChangePasswordError");
    }
}