C# ArgumentException: Invalid Arguments

Validate arguments with ArgumentException, ArgumentOutOfRangeException and ArgumentNullException.

dot net perls

ArgumentException. A method can be called with invalid arguments. An ArgumentException may be thrown in this case. Exceptions use derived types to indicate their meaning.

Notes, arguments. ArgumentException indicates that a method was called with an invalid argument. This can help keep program quality high.Exception

First example. This method receives a string parameter. In the method A, we perform 2 checks on the value of the variable argument, detecting when it is null or empty.NullEmpty String
Constructor: 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.
Result: The parameter name, which is specified as the string literal in both exception constructors, is written to the output.
C# program that uses ArgumentException 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: truncated System.ArgumentNullException: Value cannot be null. Parameter name: argument at Program.A(String argument)... System.ArgumentException: Zero-length string invalid Parameter name: argument at Program.A(String argument)... 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.
Info: Here we use null on the Dictionary indexer. This is not supported by the Dictionary type.
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.
Tip 2: In many cases, avoiding the null check and allowing the runtime itself to detect a NullReferenceException would be faster.
C# program that causes ArgumentNullException using System.Collections.Generic; class Program { static void Main() { var dictionary = new Dictionary<string, int>(); int value = dictionary[null]; } } Output Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: key

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
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.
Example: The exception is thrown explicitly with a throw statement—not by the runtime. The message helps you pinpoint the cause.
C# program that causes ArgumentOutOfRangeException class Program { static void Main() { string value = "test".Substring(-1); } } Output 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 more important.
But: With an internal method, the arguments may not need to be carefully validated because they may never be invalid.
So: For truly performance-critical methods, do not validate arguments in most cases.

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.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.