Last Updated on January 30, 2021 by sandeeppote

C# 7.0 introduced throw expression, throwing exception has always been a statement and need to call a method to throw exception. Syntax is same as you’ve always used to throw exceptions difference is it can be also placed in the conditional expressions

With c# 7.0 exceptions can be thrown directly from within expression as shown below-

 public class ThrowExpressions
 {
 public static string ThrowExpressionMethod(string someString)
 {
 return string.IsNullOrWhiteSpace(someString) ? throw new ArgumentNullException("someString is empty") : someString;
 }
 }