C# 6 exception filters and how they are much more than syntactic sugar
volatileread.com
volatileread.com
Years ago, I did .NET consulting and switched between C# and VB.NET depending on the client. Exception filters were the only feature from VB.NET that I missed when working in C#. I didn't use them often, but in some cases, they were the cleanest solution.
I've since switched to Ruby and other OSS stacks, but I'm still happy to see C# get this. It's one less argument for anyone to use VB.NET, which has far inferior syntax, in my opinion – especially when working with lambdas or events.
I was blown away by the lambda syntax. I get the need for it from a parsing perspective, but my god.
I believe this will be more effiencient than an excess of concurrent filters.
---
Found more details at [0]
Apparently the CLR runtime catches them and then causes your exception filter to return false, as if you had done so yourself.
[0] https://msdn.microsoft.com/en-US/library/ms182337%28v=vs.80%... [1] http://stackoverflow.com/questions/28879320/what-happens-if-...
Returning false in this situation feels a bit hacky IMO.
[0] http://blogs.msdn.com/b/dotnet/archive/2009/08/25/the-good-a...
I think its the only way to go. The filter function that throws could be attached to any catch block all the way up the call stack. Where exactly would you start the search for a exception handler in that case?
edit: to further clarify my objective, I was thinking of a way to make the stack traces better, without risking the maintainability of the code now that I'm aware of the difference. I guess in all it would be best to just remove it. In my case, we've found some strange things in unexpected places and I prefer to not change stuff, unless I understand how the underlying portions work.
An easy example are checking if a file exists under Windows 8, it's partly a product of the currently deficient API's but the fastest way to check if a file exists is to try to get it and catch the exception, and you'd want to return bool from a FileExists method
Syntactic sugar would be something like automatic property getters and setters in C#; they just shorten the syntax drastically and the compiler or runtime unroll your code to the traditional implementation.
In this case you can interrogate things normally bound up into the exception instance itself, like the message without having to hit the catch block to wait for it to be hydrated. Once you enter that catch block things happen in the runtime that are costly so avoiding that if possible is the best route.
Exception filters are a CLR feature that modify this process a little. Now, when an exception is thrown, it still walks the stack looking for candidate catch blocks, but whenever it identifies a candidate, it will call the exception filter function (a function from exn -> bool, where exn is the exception being caught by the Catch block). If the function returns false, the CLR will disregard this candidate catch block and continue searching. C# 6.0 merely added some syntax that exposed this functionality of the CLR, the language itself isn't doing this.
If the behavior can be expressed using previously existing syntax, then a construction is syntactic sugar. This is not true in this case. (sorry if you knew this already)