A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/dotnet/api/system.argumentoutofrangeexception below:

ArgumentOutOfRangeException Class (System) | Microsoft Learn

ArgumentOutOfRangeException Class Definition

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

public ref class ArgumentOutOfRangeException : ArgumentException
public class ArgumentOutOfRangeException : ArgumentException
[System.Serializable]
public class ArgumentOutOfRangeException : ArgumentException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArgumentOutOfRangeException : ArgumentException
type ArgumentOutOfRangeException = class
    inherit ArgumentException
type ArgumentOutOfRangeException = class
    inherit ArgumentException
    interface ISerializable
[<System.Serializable>]
type ArgumentOutOfRangeException = class
    inherit ArgumentException
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArgumentOutOfRangeException = class
    inherit ArgumentException
    interface ISerializable
Public Class ArgumentOutOfRangeException
Inherits ArgumentException
Inheritance
ArgumentOutOfRangeException
Inheritance
ArgumentOutOfRangeException
Attributes
Implements
Examples

The following example defines a class to contain information about an invited guest. If the guest is younger than 21, an ArgumentOutOfRangeException exception is thrown.

using System;
using static System.Console;

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            var guest1 = new Guest("Ben", "Miller", 17);
            WriteLine(guest1.GuestInfo);
        }
        catch (ArgumentOutOfRangeException argumentOutOfRangeException)
        {
            WriteLine($"Error: {argumentOutOfRangeException.Message}");
        }
    }
}

class Guest
{
    private const int minimumRequiredAge = 21;

    private string firstName;
    private string lastName;
    private int age;

    public Guest(string firstName, string lastName, int age)
    {
        if (age < minimumRequiredAge)
            throw new ArgumentOutOfRangeException(nameof(age), $"All guests must be {minimumRequiredAge}-years-old or older.");

        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public string GuestInfo => $"{firstName} {lastName}, {age}";
}
open System

type Guest(fName: string, lName: string, age: int) =
    let minimumRequiredAge = 21

    do if age < minimumRequiredAge then 
        raise (ArgumentOutOfRangeException(nameof age, $"All guests must be {minimumRequiredAge}-years-old or older."))

    member _.FirstName = fName
    member _.LastName = lName
    member _.GuestInfo() = $"{fName} {lName}, {age}"

try
    let guest1 = Guest("Ben", "Miller", 17);
    printfn $"{guest1.GuestInfo()}"
with
| :? ArgumentOutOfRangeException as e ->
    printfn $"Error: {e.Message}"
Module Module1
   Public Sub Main()
       Try
           Dim guest1 As Guest = New Guest("Ben", "Miller", 17)
           Console.WriteLine(guest1.GuestInfo)
       Catch outOfRange As ArgumentOutOfRangeException
           Console.WriteLine("Error: {0}", outOfRange.Message)
       End Try
   End Sub
End Module

Class Guest
    Private FirstName As String
    Private LastName As String
    Private Age As Integer

    Public Sub New(ByVal fName As String, ByVal lName As String, ByVal age As Integer)
        MyBase.New()
        FirstName = fName
        LastName = lName
        If (age < 21) Then
            Throw New ArgumentOutOfRangeException("age", "All guests must be 21-years-old or older.")
        Else
            age = age
        End If
    End Sub

    Public Function GuestInfo() As String
        Dim gInfo As String = (FirstName + (" " _
                    + (Me.LastName + (", " + Me.Age.ToString))))
        Return gInfo
    End Function
End Class

An ArgumentOutOfRangeException exception is thrown when a method is invoked and at least one of the arguments passed to the method is not null and contains an invalid value that is not a member of the set of values expected for the argument. The ParamName property identifies the invalid argument, and the ActualValue property, if a value is present, identifies the invalid value.

Typically, an ArgumentOutOfRangeException results from developer error. Instead of handling the exception in a try/catch block, you should eliminate the cause of the exception or, if the argument is returned by a method call or input by the user before being passed to the method that throws the exception, you should validate arguments before passing them to the method.

ArgumentOutOfRangeException is used extensively by:

The conditions in which an ArgumentOutOfRangeException exception is thrown include the following:

ArgumentOutOfRangeException uses the HRESULT COR_E_ARGUMENTOUTOFRANGE, which has the value 0x80131502.

For a list of initial property values for an instance of ArgumentOutOfRangeException, see the ArgumentOutOfRangeException constructors.

Constructors Properties ActualValue

Gets the argument value that causes this exception.

Data

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

(Inherited from Exception) HelpLink

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

(Inherited from Exception) HResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception) InnerException

Gets the Exception instance that caused the current exception.

(Inherited from Exception) Message

Gets the error message and the string representation of the invalid argument value, or only the error message if the argument value is null.

ParamName

Gets the name of the parameter that causes this exception.

(Inherited from ArgumentException) Source

Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception) StackTrace

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

(Inherited from Exception) TargetSite

Gets the method that throws the current exception.

(Inherited from Exception) Methods Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object) GetBaseException()

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception) GetHashCode()

Serves as the default hash function.

(Inherited from Object) GetObjectData(SerializationInfo, StreamingContext)

Obsolete.

Sets the SerializationInfo object with the invalid argument value and additional exception information.

GetType()

Gets the runtime type of the current instance.

(Inherited from Exception) MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object) ThrowIfEqual<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is equal to other.

ThrowIfGreaterThan<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is greater than other.

ThrowIfGreaterThanOrEqual<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is greater than or equal to other.

ThrowIfLessThan<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is less than other.

ThrowIfLessThanOrEqual<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is less than or equal to other.

ThrowIfNegative<T>(T, String)

Throws an ArgumentOutOfRangeException if value is negative.

ThrowIfNegativeOrZero<T>(T, String)

Throws an ArgumentOutOfRangeException if value is negative or zero.

ThrowIfNotEqual<T>(T, T, String)

Throws an ArgumentOutOfRangeException if value is not equal to other.

ThrowIfZero<T>(T, String)

Throws an ArgumentOutOfRangeException if value is zero.

ToString()

Creates and returns a string representation of the current exception.

(Inherited from Exception) Events SerializeObjectState

Obsolete.

Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception) See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4