Back
Close

Using throw-expressions in C#

gpeipman
8,203 views

Using throw-expressions in C# 7.0

C# 7.0 introduces throw expressions. We can add exception throwing to expression-bodied members, null-coalescing expressions and conditional expressions. Throw expressions are the way to tell compiler to throw exception under specific conditions like in expression bodied members or inline comparisons.

This example uses simple Person class to demonstrate different situations where throw-expressions can be used:

  • auto-property initializer statement
  • inline comparison using operator "?"
  • expression-bodied member

Check the output of demo under code window after running it.

Click Run to see demo

References

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io