concept exception filter in category c#

appears as: Exception filters, n exception filter, exception filter
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

With an exception filter, if you don’t want to handle an exception, you don’t catch it; you filter it away from your catch block to start with:

try
{
    ...                                                    #1
}
catch (WebException e)
    when (e.Status == WebExceptionStatus.ConnectFailure)   #2
{    
    ...                                                    #3
}

Beyond specific cases like this, I can see exception filters being useful in two generic use cases: retry and logging. In a retry loop, you typically want to catch the exception only if you’re going to retry the operation (if it meets certain criteria and you haven’t run out of attempts); in a logging scenario, you may never want to catch the exception but log it while it’s in-flight, so to speak. Before going into more details of the concrete use cases, let’s see what the feature looks like in code and how it behaves.

10.4.1. Syntax and semantics of exception filters

Our first full example, shown in the following listing, is simple: it loops over a set of messages and throws an exception for each of them. You have an exception filter that will catch exceptions only when the message contains the word catch. The exception filter is highlighted in bold.