Live data from Hacker News

C# 6 exception filters and how they are much more than syntactic sugar

volatileread.com

11–20 of 20 posts

Re: C# 6 exception filters and how they are much more than syntactic sugar

#11

as I see it they aren't much more than "more syntactic sugar"

Not to pile on, but: http://en.wikipedia.org/wiki/Syntactic_sugar

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)

Re: C# 6 exception filters and how they are much more than syntactic sugar

#12
> VB.NET already had this feature

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.

Re: C# 6 exception filters and how they are much more than syntactic sugar

#14
post #10

Earlier quoted context omitted.

I don't understand. Why would you have an empty catch block? He does in his example but that's an example. I cannot see why you'd even write a catch block if it was empty in production code (unless you're trying to do some error-and-continue pattern, which is rare/mostly an anti-pattern).

Or just supress it, for some methods you might want to return a null value/"null object"/bool rather than have an exception cascade up the chain and risk crashing the application. I think Exceptions are a bit problematic as a concept but then again so are null values :) 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…

After many years I still don't really grok the idea behind exceptions, and I get this feeling that I'm not alone with that. There probably is an extremely smart idea supporting this whole "handle all errors with exceptions" style, but I haven't seen any good explanation yet; instead what I see most often is a case of Stockholm syndrome - "it's in the language, it's The $LANGUAGE Style, it must be Good!".

Re: C# 6 exception filters and how they are much more than syntactic sugar

#15

What happens when the exception test expression throws an exception?

edit: Apparently [0] is for VB.net and C++ but I would think including VB.net would mean the CLR. If so, then C# should be the same. A further SO post on the topic with no official sources confirms the same at [1]

---

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-...

Re: C# 6 exception filters and how they are much more than syntactic sugar

#16
post #15

What happens when the exception test expression throws an exception?

edit: Apparently [0] is for VB.net and C++ but I would think including VB.net would mean the CLR. If so, then C# should be the same. A further SO post on the topic with no official sources confirms the same at [1] --- 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/ms18…

Good find, thanks!

Returning false in this situation feels a bit hacky IMO.

Re: C# 6 exception filters and how they are much more than syntactic sugar

#17
post #15

Earlier quoted context omitted.

edit: Apparently [0] is for VB.net and C++ but I would think including VB.net would mean the CLR. If so, then C# should be the same. A further SO post on the topic with no official sources confirms the same at [1] --- 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/ms18…

Good find, thanks! Returning false in this situation feels a bit hacky IMO.

I found another interesting gotcha in regards to the filter step of exception handling described at [0]. Its in VB but describes the CLR two-pass exception handling and how filter functions and finally blocks interact with regards to state.

[0] http://blogs.msdn.com/b/dotnet/archive/2009/08/25/the-good-a...

Re: C# 6 exception filters and how they are much more than syntactic sugar

#18
post #15

Earlier quoted context omitted.

edit: Apparently [0] is for VB.net and C++ but I would think including VB.net would mean the CLR. If so, then C# should be the same. A further SO post on the topic with no official sources confirms the same at [1] --- 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/ms18…

Good find, thanks! Returning false in this situation feels a bit hacky IMO.

To respond to your point,

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?

Re: C# 6 exception filters and how they are much more than syntactic sugar

#20

> VB.NET already had this feature 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…

I worked on a VB.Net project once and went to use LINQ...

I was blown away by the lambda syntax. I get the need for it from a parsing perspective, but my god.

Post reply on HN