Tracks
/
C#
C#
/
Syllabus
/
Exception Filtering
Ex

Exception Filtering in C#

1 exercise

About Exception Filtering

when is the keyword in filtering exceptions. It is placed after the catch statement and can take a boolean expression containing any values in scope at the time. They don't just have to be members of the exception itself. If the type of the exception matches and the expression evaluates to true then the block associated with that catch statement is executed otherwise the next catch statement, if any, is checked.

try
{
    // do stuff
}
catch (Exception ex) when (ex.Message != "")
{
    // output the message when it is not empty
}
catch (Exception ex)
{
    // show stack trace or something.
}
Edit via GitHub The link opens in a new window or tab

Learn Exception Filtering