ArgumentException. A C# method can be called with invalid arguments—this may cause an ArgumentException. In this language, exceptions use derived types to indicate their meaning.
Exception meanings. ArgumentException indicates that a method was called with an invalid argument. This can help improve program quality.
ArgumentException. Here the Test() method receives a string parameter. In it, we perform 2 checks on the value of the variable argument, detecting when it is null or empty.
Detail When using the ArgumentNullException constructor, you can pass a string literal indicating the null argument.
Tip You can use the same pattern on ArgumentException, which indicates a general argument-related exception.
Info For clearer code, consider the ArgumentNullException ThrowIfNull method shown in a further example on this page.
using System;
class Program
{
static void Main()
{
// Demonstrate the argument null exception.
try
{
Test(null);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// Demonstrate the general argument exception.
try
{
Test("");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// No exception.
Console.WriteLine(Test("test"));
}
static int Test(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;
}
}System.ArgumentNullException: Value cannot be null. (Parameter 'argument')
at Program.Test(String argument) in .../Program.cs:line 34
at Program.Main() in .../Program.cs:line 10
System.ArgumentException: Zero-length string invalid (Parameter 'argument')
at Program.Test(String argument) in .../Program.cs:line 39
at Program.Main() in .../Program.cs:line 19
4
ArgumentNullException. This is thrown by code that checks arguments for null. Usually, the method would fail with a NullReferenceException if the check was removed.
Here We use null on the Dictionary indexer—this is not supported by the Dictionary type.
using System.Collections.Generic;
class Program
{
static void Main()
{
var dictionary = new Dictionary<string, int>();
int value = dictionary[null];
}
}Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: key
ThrowIfNull. We can use the ThrowIfNull static method to reduce code size that checks for null arguments. This method throws an exception if its argument is null.
using System;
class Program
{
static void Test(string value)
{
// Ensure argument is not null.
ArgumentNullException.ThrowIfNull(value);
Console.WriteLine($"Value is {value}");
}
static void Main()
{
Test("cat");
Test(null);
}
}Value is cat
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'value')
at System.ArgumentNullException.Throw(String paramName)
at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
at Program.Test(String value) in .../Program.cs:line 8
at Program.Main() in .../Program.cs:line 15
ArgumentOutOfRangeException. This program causes an ArgumentOutOfRangeException to be thrown by the Substring method. Substring requires its argument to be greater than or equal to zero.
class Program
{
static void Main()
{
string value = "test".Substring(-1);
}
}Unhandled Exception:
System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
Parameter name: startIndex
A discussion. There are places where you should carefully validate arguments. If you have a method that is called in many different places, then validating arguments is important.
A summary. We looked at the ArgumentException and ArgumentNullException types. The type names are a way to encode the meaning of the exception's cause. They indicate an argument problem.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 20, 2022 (new example).