Advertisement
Intermediate C#, Exception Handling

What are exception filters (when) and why use them?

Short answer

A when clause lets a catch run only if a condition is true, without unwinding the stack for non-matching cases, which aids logging and selective handling.

Save C# mock interview
Advertisement
AModel answer
A when clause lets a catch run only if a condition is true, without unwinding the stack for non-matching cases, which aids logging and selective handling.
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
Real-world example
Handling only 404s from an HTTP call while letting other status codes propagate.