Table of Contents

Class TypeArgumentOutOfRangeException

Namespace
Cuemon
Assembly
Cuemon.Kernel.dll

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

public class TypeArgumentOutOfRangeException : ArgumentOutOfRangeException, ISerializable
Inheritance
TypeArgumentOutOfRangeException
Implements
Inherited Members

Examples

The following example demonstrates how to throw a when an enum-based type argument is outside the valid range.

using System;
using Cuemon;

namespace Contoso.Logging;

public sealed class TypeArgumentOutOfRangeExceptionExample
{
    public static void Run()
    {
        LogLevel parsed = ParseEnum<LogLevel>("Warning");
        Console.WriteLine(parsed);

        try
        {
            ParseEnum<int>("1");
        }
        catch (TypeArgumentOutOfRangeException ex)
        {
            Console.WriteLine($"{ex.ParamName}: {ex.ActualValue}");
        }
    }

    private static TEnum ParseEnum<TEnum>(string text)
        where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
        {
            throw new TypeArgumentOutOfRangeException(
                nameof(TEnum),
                typeof(TEnum),
                "Type arguments must be enum types.");
        }

        return (TEnum)Enum.Parse(typeof(TEnum), text, ignoreCase: true);
    }

    private enum LogLevel
    {
        Debug,
        Information,
        Warning,
        Error
    }
}

Constructors

TypeArgumentOutOfRangeException()

Initializes a new instance of the TypeArgumentOutOfRangeException class.

public TypeArgumentOutOfRangeException()

TypeArgumentOutOfRangeException(string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

public TypeArgumentOutOfRangeException(string typeParamName)

Parameters

typeParamName string

The name of the type parameter that caused the exception.

TypeArgumentOutOfRangeException(string, Exception)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

public TypeArgumentOutOfRangeException(string message, Exception innerException)

Parameters

message string

The message that describes the error.

innerException Exception

The exception that is the cause of the current exception. If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.

TypeArgumentOutOfRangeException(string, object, string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

public TypeArgumentOutOfRangeException(string typeParamName, object actualValue, string message)

Parameters

typeParamName string

The name of the type parameter that caused the exception.

actualValue object

The value of the argument that causes this exception.

message string

The message that describes the error.

TypeArgumentOutOfRangeException(string, string)

Initializes a new instance of the TypeArgumentOutOfRangeException class.

public TypeArgumentOutOfRangeException(string typeParamName, string message)

Parameters

typeParamName string

The name of the type parameter that caused the exception.

message string

The message that describes the error.