23

TIL: C# has a "Catch When" syntax to help you filter exceptions. It already allows you to filter by Exception type, but this is news to me since it allows for finer filtering like:

try
{
//Shit code that will throw an exceptions more than Hillary's tantrums about the elections
}
catch (ExceptionType ex) when (ex.ErrorCode = "0x696666")
{
//Log this fuck up
}
catch (Exception ex)
{
//More logs
}
finally
{
//Run code that doesn't depend on the successful execution of prev code
}

I love C# and use it every single day, but this "When" keyword in Try...Catch...Finally blocks is new to me and will be interesting to start using it now :)

Comments
Add Comment