While doing a code review I noticed the following code:
In the code above we handle 2 exceptional cases. One specific case when the Task
is cancelled and an OperationCancelledException
is thrown. In that case we ignore the exception and decide to not log it anywhere (we can discuss if this is a good idea or not, but that is not what I want to talk about). And a second exceptional case when any other error type occurs we write it to our logs.
Although there is nothing wrong with the code above, we can further simplify it by introducting an Exception Filter. This allows us to catch and handle exceptions based on requirements you define for the exception. These filters use the catch
statement with the when
keyword:
Nice!
Another trick I noticed in the documentation where exception filters are useful is when you want to examine all exceptions but not handle them. This is useful for example when you want to log the exception: