Table of Contents

Class ExceptionMessages

Namespace
System
Assembly
Cobilas.Core.dll

Provides utility methods for throwing standard exceptions with consistent messages.

public static class ExceptionMessages
Inheritance
ExceptionMessages
Inherited Members

Remarks

This static class contains methods that replicate the behavior of exception-throwing methods introduced in newer .NET versions, providing backward compatibility for older frameworks. Each method conditionally uses the framework's native method when available.

Methods

ThrowIf(bool, object)

Throws an ObjectDisposedException if the specified condition is true.

public static void ThrowIf(bool condition, object instance)

Parameters

condition bool

The condition to evaluate.

instance object

The object whose type's full name should be included in any resulting ObjectDisposedException.

Exceptions

ObjectDisposedException

The condition is true.

ThrowIf(bool, Type)

Throws an ObjectDisposedException if the specified condition is true.

public static void ThrowIf(bool condition, Type type)

Parameters

condition bool

The condition to evaluate.

type Type

The type whose full name should be included in any resulting ObjectDisposedException.

Exceptions

ObjectDisposedException

The condition is true.

ThrowIfEqual<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is equal to other.

public static void ThrowIfEqual<T>(T value, T other, string? paramName = null) where T : IEquatable<T>

Parameters

value T

The argument to validate as not equal to other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement IEquatable<T> for comparison.

Exceptions

ArgumentOutOfRangeException

value is equal to other.

ThrowIfGreaterThanOrEqual<T>(T, T, string?)

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

public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, string? paramName = null) where T : IComparable<T>, IEquatable<T>

Parameters

value T

The argument to validate as less than other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement both IComparable<T> and IEquatable<T>. On .NET 8.0 and above, uses the framework's built-in method; otherwise, combines greater-than and equality checks.

Exceptions

ArgumentOutOfRangeException

value is greater than or equal to other.

ThrowIfGreaterThan<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is greater than other.

public static void ThrowIfGreaterThan<T>(T value, T other, string? paramName = null) where T : IComparable<T>

Parameters

value T

The argument to validate as less or equal than other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement IComparable<T> for comparison.

Exceptions

ArgumentOutOfRangeException

value is greater than other.

ThrowIfLessThanOrEqual<T>(T, T, string?)

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

public static void ThrowIfLessThanOrEqual<T>(T value, T other, string? paramName = null) where T : IComparable<T>, IEquatable<T>

Parameters

value T

The argument to validate as greater than other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement both IComparable<T> and IEquatable<T>. On .NET 8.0 and above, uses the framework's built-in method; otherwise, combines less-than and equality checks.

Exceptions

ArgumentOutOfRangeException

value is less than or equal to other.

ThrowIfLessThan<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is less than other.

public static void ThrowIfLessThan<T>(T value, T other, string? paramName = null) where T : IComparable<T>

Parameters

value T

The argument to validate as greater than or equal than other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement IComparable<T> for comparison.

Exceptions

ArgumentOutOfRangeException

value is less than other.

ThrowIfNegative(decimal, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(decimal value, string? paramName = null)

Parameters

value decimal

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(double, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(double value, string? paramName = null)

Parameters

value double

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(short, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(short value, string? paramName = null)

Parameters

value short

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(int, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(int value, string? paramName = null)

Parameters

value int

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(long, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(long value, string? paramName = null)

Parameters

value long

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(sbyte, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(sbyte value, string? paramName = null)

Parameters

value sbyte

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegative(float, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative(float value, string? paramName = null)

Parameters

value float

The argument to validate as non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative.

ThrowIfNegativeOrZero(byte, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(byte value, string? paramName = null)

Parameters

value byte

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(decimal, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(decimal value, string? paramName = null)

Parameters

value decimal

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(double, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(double value, string? paramName = null)

Parameters

value double

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(short, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(short value, string? paramName = null)

Parameters

value short

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(int, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(int value, string? paramName = null)

Parameters

value int

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(long, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(long value, string? paramName = null)

Parameters

value long

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(sbyte, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(sbyte value, string? paramName = null)

Parameters

value sbyte

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(float, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(float value, string? paramName = null)

Parameters

value float

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(ushort, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(ushort value, string? paramName = null)

Parameters

value ushort

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(uint, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(uint value, string? paramName = null)

Parameters

value uint

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNegativeOrZero(ulong, string?)

Throws an ArgumentOutOfRangeException if value is negative or zero.

public static void ThrowIfNegativeOrZero(ulong value, string? paramName = null)

Parameters

value ulong

The argument to validate as non-zero or non-negative.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is negative or zero.

ThrowIfNotEqual<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is not equal to other.

public static void ThrowIfNotEqual<T>(T value, T other, string? paramName = null) where T : IEquatable<T>

Parameters

value T

The argument to validate as equal to other.

other T

The value to compare with value.

paramName string

The name of the parameter with which value corresponds.

Type Parameters

T

The type of the values to compare.

Remarks

This method requires T to implement IEquatable<T> for comparison.

Exceptions

ArgumentOutOfRangeException

value is not equal to other.

ThrowIfNull(nint, string?)

Throws an ArgumentNullException if argument is null (IntPtr.Zero).

public static void ThrowIfNull(nint argument, string? paramName = null)

Parameters

argument nint

The pointer argument to validate as non-null.

paramName string

The name of the parameter with which argument corresponds.

Remarks

Validates that an nint is not zero, which represents a null pointer.

Exceptions

ArgumentNullException

argument is Zero.

ThrowIfNull(object?, string?)

Throws an ArgumentNullException if argument is null.

public static void ThrowIfNull(object? argument, string? paramName = null)

Parameters

argument object

The reference type argument to validate as non-null.

paramName string

The name of the parameter with which argument corresponds.

Remarks

On .NET 7.0 and above, this method uses the framework's built-in 'ArgumentNullException.ThrowIfNull(object?, string?)' method.

Exceptions

ArgumentNullException

argument is null.

ThrowIfNullOrEmpty(string?, string?)

Throws an exception if argument is null or empty.

public static void ThrowIfNullOrEmpty(string? argument, string? paramName = null)

Parameters

argument string

The string argument to validate as non-null and non-empty.

paramName string

The name of the parameter with which argument corresponds.

Remarks

This method checks for both null and empty strings. On .NET 7.0 and above, uses the framework's built-in method.

Exceptions

ArgumentNullException

argument is null.

ArgumentException

argument is empty.

ThrowIfNullOrWhiteSpace(string?, string?)

Throws an exception if argument is null, empty, or consists only of white-space characters.

public static void ThrowIfNullOrWhiteSpace(string? argument, string? paramName = null)

Parameters

argument string

The string argument to validate.

paramName string

The name of the parameter with which argument corresponds.

Remarks

This method performs a more stringent validation than ThrowIfNullOrEmpty(string?, string?) by also checking for whitespace-only strings.

Exceptions

ArgumentNullException

argument is null.

ArgumentException

argument is empty or consists only of white-space characters.

ThrowIfZero(byte, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(byte value, string? paramName = null)

Parameters

value byte

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(decimal, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(decimal value, string? paramName = null)

Parameters

value decimal

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(double, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(double value, string? paramName = null)

Parameters

value double

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(short, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(short value, string? paramName = null)

Parameters

value short

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(int, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(int value, string? paramName = null)

Parameters

value int

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(long, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(long value, string? paramName = null)

Parameters

value long

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(sbyte, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(sbyte value, string? paramName = null)

Parameters

value sbyte

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(float, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(float value, string? paramName = null)

Parameters

value float

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(ushort, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(ushort value, string? paramName = null)

Parameters

value ushort

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(uint, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(uint value, string? paramName = null)

Parameters

value uint

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.

ThrowIfZero(ulong, string?)

Throws an ArgumentOutOfRangeException if value is zero.

public static void ThrowIfZero(ulong value, string? paramName = null)

Parameters

value ulong

The argument to validate as non-zero.

paramName string

The name of the parameter with which value corresponds.

Exceptions

ArgumentOutOfRangeException

value is zero.