Class

The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.

Inheritance

Remarks

The ArgumentOutOfRangeException class represents errors that occur when an argument passed to a method is not valid because its value is outside the expected range. This is a common error in programming that signifies an attempt to use a value that is too large, too small, or otherwise invalid for the operation being performed.

When this exception is thrown, it typically includes the name of the argument that caused the error, the invalid value, and a descriptive message explaining why the value is out of range.

Constructors

Properties

Methods

Example

The following example demonstrates how to throw an ArgumentOutOfRangeException when an argument is outside the expected range.


public void SetTemperature(int temperature)
{
    if (temperature < -273 || temperature > 10000)
    {
        throw new ArgumentOutOfRangeException(nameof(temperature), "Temperature must be between -273 and 10000 degrees Celsius.");
    }
    this.temperature = temperature;
}
                    

Constructor

ArgumentOutOfRangeException()

Initializes a new instance of the ArgumentOutOfRangeException class.

Constructor

ArgumentOutOfRangeException(string paramName)

Initializes a new instance of the ArgumentOutOfRangeException class with a specified parameter name and an error message that explains the reason for this exception.

Parameters

Name Description
paramName The name of the parameter that caused the out-of-range error.

Constructor

ArgumentOutOfRangeException(string paramName, string message)

Initializes a new instance of the ArgumentOutOfRangeException class with a specified parameter name and an error message that explains the reason for this exception.

Parameters

Name Description
paramName The name of the parameter that caused the out-of-range error.
message An error message that explains the reason for this exception.

Constructor

ArgumentOutOfRangeException(string paramName, object actualValue, string message)

Initializes a new instance of the ArgumentOutOfRangeException class with the parameter name, the value of the argument, and an error message.

Parameters

Name Description
paramName The name of the parameter that caused the out-of-range error.
actualValue The value of the argument that is out of range.
message An error message that explains the reason for this exception.

Constructor

ArgumentOutOfRangeException(string paramName, object actualValue, string message, Exception innerException)

Initializes a new instance of the ArgumentOutOfRangeException class with the parameter name, the value of the argument, an error message, and the inner exception that is the cause of this exception.

Parameters

Name Description
paramName The name of the parameter that caused the out-of-range error.
actualValue The value of the argument that is out of range.
message An error message that explains the reason for this exception.
innerException The exception that is the cause of the current exception. If innerException is not null, the current exception is raised in a catch block that handles innerException.

Constructor

ArgumentOutOfRangeException(string paramName, Exception innerException)

Initializes a new instance of the ArgumentOutOfRangeException class with the parameter name and a reference to the inner exception that is the cause of this exception.

Parameters

Name Description
paramName The name of the parameter that caused the out-of-range error.
innerException The exception that is the cause of the current exception. If innerException is not null, the current exception is raised in a catch block that handles innerException.

Properties

ActualValue

Gets the value of the argument that causes this exception.

object ActualValue { get; }

Message

Gets a message that describes the current exception and the cause of the error.

string Message { get; }

ParamName

Gets the name of the parameter that causes this exception.

string ParamName { get; }

InnerException

Gets a reference to the inner exception that is the cause of this exception.

Exception InnerException { get; }

Gets or sets a link to the help file associated with this exception.

string HelpLink { get; set; }

StackTrace

Gets a string representation of the immediate frames of the call stack.

string StackTrace { get; }

TargetSite

Gets the method that throws the current exception.

MethodBase TargetSite { get; }

Data

Gets a collection of key/value pairs that provide additional user-defined information about the exception.

IDictionary Data { get; }

Methods

GetObjectData

Sets the SerializationInfo object with information about the exception.

void GetObjectData(SerializationInfo info, StreamingContext context)

Parameters

Name Description
info The SerializationInfo object that holds the serialized object data about the exception.
context Information about the source and destination of a serialized stream and the context that is provided during deserialization.

ToString

Returns the fully qualified name of the exception class and the error message, the name of the inner exception, and the stack trace.

string ToString()