Computer >> Computer tutorials >  >> Programming >> C#

ArgumentNullException in C#


The exception thrown when a null reference is passed to a method that does not accept it as a valid argument.

Let us see an example.

When we set a null parameter to int.Parse() method, then ArgumentNullException is thrown as shown below −

Example

using System;
class Demo {
   static void Main() {
      string val = null;
      int res = int.Parse(val); // error is thrown
   }
}

Output

The following error is thrown when the above program is compiled since we have passed a null value.

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.