Exception Filters in C# 6.0

In the last post inspired by The Future of C# talk at GOTO, I introduced you to Declaration Expressions and Static Using. In this post I will take a look at Exception Filters in C# 6.0. Exception Filters is one of two improvements for exception handling, the other being able to do await calls from catch blogs. Await in catch blocks is something that should have been in the framework from the time async/await where originally introduced, why I don’t want to pay too much attention to it in this post. Makes sense implementing it.

So what’s this exception filtering all about? You probably wrote code looking similar to this at some point:

The catch blog in this example switches over a HTTP status code and provide different behavior based on the status code. Notice that all handling of the HttpException is located inside a single catch block.

The above example written using Exception Filters could look something like this:

In the updated example we define a catch block for each condition in the switch block. The new part is the if statement added to the right of each catch specification. The idea here is to tell .NET to only execute each catch block if both the exception type matches and the statement inside the if match. Like usual .NET will evaluate each catch block from the top, falling through each block until both exception type and filter matches. In case the HTTP status code is different than 400, 404 and 500, the generic catch block is executed.

Let’s see how this code looks decompiled:

Talking about ugly code! Neither ILSpy nor tryroslyn.azurewebsites.net is able to show the entire code (“Not Found” etc. is missing from the decompiled code), but looking through the generated IL, the whole thing is still there.