public class ArgumentException : SystemException
Object
Exception
SystemException
ArgumentException
mscorlib
BCL
Represents the error that occurs when an argument passed to a method is invalid.
ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the method's parameter specification.[Note: The Base Class Library includes three derived types:
When appropriate, use these types instead of ArgumentException.
]
The following example demonstrates an error that causes a ArgumentException exception to be thrown by the system.
using System; public class MyClass {} public class ArgExceptionExample { public static void Main() { MyClass my = new MyClass(); string s = "sometext"; try { int i = s.CompareTo(my); } catch (ArgumentException e) { Console.WriteLine("Error: {0}",e); } } }The output is
Error: System.ArgumentException: Object must be of type String.
at System.String.CompareTo(Object value)
at ArgExceptionExample.Main()
System Namespace
ArgumentException Constructors
ArgumentException() Constructor
ArgumentException(System.String) Constructor
ArgumentException(System.String, System.Exception) Constructor
ArgumentException(System.String, System.String, System.Exception) Constructor
ArgumentException(System.String, System.String) Constructor
ArgumentException Properties
public ArgumentException();
Constructs and initializes a new instance of the ArgumentException class.
This constructor initializes the System.ArgumentException.Message property of the new instance to a system-supplied message that describes the error, such as "An invalid argument was specified." This message takes into account the current system culture.The System.ArgumentException.InnerException property is initialized to
null
.
System.ArgumentException Class, System Namespace
public ArgumentException(string message);
Constructs and initializes a new instance of the ArgumentException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.ArgumentException.Message property of the new instance using message. If message isnull
, the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.InnerException and System.ArgumentException.ParamName properties are initialized tonull
.
System.ArgumentException Class, System Namespace
public ArgumentException(string message, Exception innerException);
Constructs and initializes a new instance of the ArgumentException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException .
This constructor initializes the System.ArgumentException.Message property of the new instance using message, and the System.ArgumentException.InnerException property using innerException. If message isnull
, the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.ParamName property is initialized tonull
.[Note: For information on inner exceptions, see System.Exception.InnerException.]
System.ArgumentException Class, System Namespace
public ArgumentException(string message, string paramName, Exception innerException);
Constructs and initializes a new instance of the ArgumentException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- paramName
- A String that contains the name of the parameter that caused the current exception.
- innerException
- An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException.
This constructor initializes the System.ArgumentException.Message property of the new instance using message, the System.ArgumentException.ParamName property using paramName, and the System.ArgumentException.InnerException property using innerException. If message isnull
, the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For information on inner exceptions, see System.Exception.InnerException.]
System.ArgumentException Class, System Namespace
public ArgumentException(string message, string paramName);
Constructs and initializes a new instance of the ArgumentException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- paramName
- A String that contains the name of the parameter that caused the exception.
This constructor initializes the System.ArgumentException.Message property of the new instance using message, and the System.ArgumentException.ParamName property using paramName. If message isnull
, the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.InnerException property is initialized tonull
.
System.ArgumentException Class, System Namespace
public virtual string ParamName { get; }
Gets the name of the parameter that caused the current Exception.
A String that contains the name of the parameter that caused the current Exception, ornull
if no value was specified to the constructor for the current instance.
This property is read-only.[Behaviors: The System.ArgumentException.ParamName property returns the same value as was passed into the constructor.]
[Overrides: Override the System.ArgumentException.ParamName property to customize the content or format of the parameter name. ]
[Usage: Use this property to retrieve the name of the invalid parameter. ]
System.ArgumentException Class, System Namespace