C# language feature:
applies to
benefit
how to use
true
following when
or if
keywords.note
note 2
example:
public void Exception_Filters_Example()
{
try {
throw new Exception("this will fail!");
}
catch (Exception e) when (LogException(e))
{
// This is never reached as logexception always returns false!
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Exceptions processed here.
}
}
public static bool LogException(Exception e)
{
Console.Error.WriteLine(@"Exceptions are always recorded: {e}");
return false; // returning false forces move to next exception in catch block
}