Home
C#
ArgumentException Examples
This page was last reviewed on Oct 20, 2022.
Dot Net Perls
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.
Exception
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.
null
string.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.
Dictionary
Indexer
And The indexer compiles to the get_Item method. Internally, get_Item eventually uses the statement "throw new ArgumentNullException".
Tip The ArgumentNullException can be understood as an exception that is thrown by user code, not the runtime.
Detail Avoiding the null check and allowing the runtime itself to detect a NullReferenceException would be faster.
NullReferenceException
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.
String Substring
Tip Internally, Substring checks its argument for a negative value. With this exception, it alerts you to an invalid value.
And This error is helpful. It makes your program easier to fix. It pinpoints the nature of your logical error.
Detail The exception is thrown explicitly with a throw statement—not by the runtime. The message helps you pinpoint the cause.
throw
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).
Home
Changes
© 2007-2024 Sam Allen.