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
meaningsArgumentException
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.
ArgumentNullException
constructor, you can pass a string
literal indicating the null
argument.ArgumentException
, which indicates a general argument-related exception.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.
null
on the Dictionary
indexer—this is not supported by the Dictionary
type.get_Item
method. Internally, get_Item
eventually uses the statement "throw new ArgumentNullException
".ArgumentNullException
can be understood as an exception that is thrown by user code, not the runtime.null
check and allowing the runtime itself to detect a NullReferenceException
would be faster.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.
Substring
checks its argument for a negative value. With this exception, it alerts you to an invalid value.class Program { static void Main() { string value = "test".Substring(-1); } }Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex
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.
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.