throw has been a keyword since the first version of C#. The way the developer interact with it hasn’t been touched ever since.
Sure, lots of new features has been brought on board since but…
throw? Never touched until now.
I do not really need to tell you how to throw an exception. There’s 2 ways.
public void Something()
And that was it. If you wanted to throw an exception any other way,
All you see below are invalid before C# 7.0.
public class Dummy
Oh so many of them. Here’s what was included in the previous snippet of code.
You can now throw from:
- Null-Coalescing operator. The
??operator used to provide an alternative value. Now you can use it throw when value shouldn’t be null.
- Lambda. Still don’t understand exactly why it wasn’t allow before but now? Totally legit.
- Conditional Operator. Throw from the left or the right of the
?:operator anytime you feel like it. Before? Not allowed.
- Expression Body. In fact, any expression body will support it.
Where you still can’t throw (that I verified):
if(condition)statement. Cannot be used in the condition of a
ifstatement. Even if it’s using a null-coalescing operator, it won’t work and is not valid syntax.
Are you going to use it?
I know that not everyone will necessarily use all of these new form of expression body. But I’m interested in your opinion.
So please leave me a comment and let me know if it’s something that will simplify your life or, at least, your code.