C# 6 introduced the when keyword. The when keyword is a way to filter an exception in a catch block by some specific criteria. It was later extended to case statements in C# 7. I’ll focus on when to use it in exceptions in this post and address its use in a case statement in another post.
Catch Without When
Let’s say you want to handle the same type of exceptions using different criteria. A common scenario for this is an HttpException. When you catch an HttpException, the response code is available on the ResponseCode property. Depending on the response code, you may want to take different actions.