
A method can be called with invalid arguments. An ArgumentException may be thrown in this case. Exceptions use derived types to indicate their meaning, but this doesn't give them extra abilities. Semantically, ArgumentException indicates that a method was called with an invalid argument.
This C# example shows the ArgumentException being thrown. ArgumentException helps validate arguments.

In this example, we describe a method that receives one formal parameter, a string type with the identifier "argument". In the method A, we perform two checks on the value of the variable argument, detecting when it is null or has zero characters in its buffer.
Constructor. When using the ArgumentNullException constructor, you can pass a string literal that is equal to the variable name that was null. This will aid debugging. You can use the same pattern on ArgumentException, which indicates a general argument-related exception.
Program that uses ArgumentException [C#]
using System;
class Program
{
static void Main()
{
// Demonstrate the argument null exception.
try
{
A(null);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// Demonstrate the general argument exception.
try
{
A("");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// Flow path without exception.
Console.WriteLine(A("test"));
}
static int A(string argument)
{
// Handle null argument.
if (argument == null)
{
throw new ArgumentNullException("argument");
}
// Handle invalid argument.
if (argument.Length == 0)
{
throw new ArgumentException("Zero-length string invalid", "argument");
}
return argument.Length;
}
}
Output
Output truncated.
System.ArgumentNullException: Value cannot be null.
Parameter name: argument
at Program.A(String argument) in...
System.ArgumentException: Zero-length string invalid
Parameter name: argument
at Program.A(String argument) in...
4Parameter name. The most interesting part of this example is that the parameter name, which is specified as the string literal in both exception constructors, is written to the output. The argument name is important as a debugging aid. For example, maybe the method A would receive two strings, and neither of them could be validly null—the argument name would be necessary to ensure fast debugging.

When to validate arguments? In programming, there are some cases where you should validate arguments more carefully than others. If you have a method that is called in many different places or any external places written by external developers, then validating arguments is more important.
Often, you will have an internal method and the arguments on it do not need to be carefully validated because they will not ever be invalid. For truly performance-critical methods, do not validate arguments in most cases.

We looked at the ArgumentException and ArgumentNullException types in the C# language. While in most parts of programming the types of variables indicates their capabilities, in the exception type hierarchy, the types are a way to encode the meaning of the exception's cause. In other words, ArgumentException and ArgumentNullException are more semantically rich and aid in efficient debugging.
ArgumentNullException Exception Handling