System.ArgumentException
The exception that is thrown when an argument to a method is not valid.
Syntax
[Serializable]
public class ArgumentException : SystemException
Inheritance
E Object
E ArgumentException
Remarks
The ArgumentException class represents the errors that occur when a method is passed an argument that is invalid. This exception is typically thrown when a value is outside the range of valid values for that argument, or when an argument is not of the expected type. It is important to catch and handle ArgumentException to gracefully manage invalid input and prevent application crashes. The Properties section below provides details on how to access information about the exception, such as the name of the invalid argument and the error message.
Constructors
Constructor | Description |
---|---|
ArgumentException() | Initializes a new instance of the ArgumentException class. |
ArgumentException(String) | Initializes a new instance of the ArgumentException class with a specified error message. |
ArgumentException(String, String) | Initializes a new instance of the ArgumentException class with a specified error message and the name of the parameter that causes this exception. |
ArgumentException(String, String, Exception) | Initializes a new instance of the ArgumentException class with a specified error message, the name of the parameter that causes this exception, and a reference to the inner exception that is the cause of this exception. |
ArgumentException(SerializationInfo, StreamingContext) | Initializes a new instance of the ArgumentException class with serialized data. |
Properties
Property | Description | |
---|---|---|
ParamName
|
String |
Gets the name of the argument that causes this exception. |
Message
|
String |
Gets an informative message that describes the error, including the name of the parameter that caused the error. (Inherited from Exception ) |
InnerException
|
Exception |
Gets the exception that is the cause of the current exception. (Inherited from Exception ) |
StackTrace
|
String |
Gets a string representation of the immediate frames of the call stack. (Inherited from Exception ) |
HelpLink
|
String |
Gets or sets a link to the help file that is associated with this exception. (Inherited from Exception ) |
Source
|
String |
Gets or sets the name of the application or the object that causes the error. (Inherited from Exception ) |
Data
|
IDictionary |
Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception ) |
HResult
|
Int32 |
Gets or sets HRESULT, which is a coded numerical value that is assigned to a specific exception. (Inherited from Exception ) |
TargetSite
|
MethodBase |
Gets the method that throws the current exception. (Inherited from Exception ) |
Methods
Method | Description |
---|---|
GetBaseException() | When overridden in a derived class, returns the Exception that is the root cause of the current exception. (Inherited from Exception ) |
GetObjectData(SerializationInfo, StreamingContext) | When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception ) |
GetType() | Gets the type of the current instance. (Inherited from Object ) |
ToString() | Overrides the default exception message to include the parameter name if it is available. (Inherited from Exception ) |
Usage Example
The following C# code demonstrates how to throw and catch an ArgumentException
.
public void SetAge(int age)
{
if (age < 0)
{
throw new ArgumentException("Age cannot be negative.", nameof(age));
}
// ... rest of the logic
}
try
{
SetAge(-5);
}
catch (ArgumentException ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"Invalid argument: {ex.ParamName}");
}