C# 7.0 – Throw expressions

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;
 }
 }

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s