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
conditionboolThe condition to evaluate.
instanceobjectThe object whose type's full name should be included in any resulting ObjectDisposedException.
Exceptions
- ObjectDisposedException
The
conditionis true.
ThrowIf(bool, Type)
Throws an ObjectDisposedException if the specified condition is true.
public static void ThrowIf(bool condition, Type type)
Parameters
conditionboolThe condition to evaluate.
typeTypeThe type whose full name should be included in any resulting ObjectDisposedException.
Exceptions
- ObjectDisposedException
The
conditionis 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
valueTThe argument to validate as not equal to
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe type of the values to compare.
Remarks
This method requires T to implement IEquatable<T> for comparison.
Exceptions
- ArgumentOutOfRangeException
valueis equal toother.
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
valueTThe argument to validate as less than
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe 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
valueis greater than or equal toother.
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
valueTThe argument to validate as less or equal than
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe type of the values to compare.
Remarks
This method requires T to implement IComparable<T> for comparison.
Exceptions
- ArgumentOutOfRangeException
valueis greater thanother.
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
valueTThe argument to validate as greater than
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe 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
valueis less than or equal toother.
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
valueTThe argument to validate as greater than or equal than
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe type of the values to compare.
Remarks
This method requires T to implement IComparable<T> for comparison.
Exceptions
- ArgumentOutOfRangeException
valueis less thanother.
ThrowIfNegative(decimal, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(decimal value, string? paramName = null)
Parameters
valuedecimalThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(double, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(double value, string? paramName = null)
Parameters
valuedoubleThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(short, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(short value, string? paramName = null)
Parameters
valueshortThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(int, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(int value, string? paramName = null)
Parameters
valueintThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(long, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(long value, string? paramName = null)
Parameters
valuelongThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(sbyte, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(sbyte value, string? paramName = null)
Parameters
valuesbyteThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegative(float, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void ThrowIfNegative(float value, string? paramName = null)
Parameters
valuefloatThe argument to validate as non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis negative.
ThrowIfNegativeOrZero(byte, string?)
Throws an ArgumentOutOfRangeException if value is negative or zero.
public static void ThrowIfNegativeOrZero(byte value, string? paramName = null)
Parameters
valuebyteThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valuedecimalThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valuedoubleThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueshortThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueintThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valuelongThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valuesbyteThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valuefloatThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueushortThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueuintThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueulongThe argument to validate as non-zero or non-negative.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis 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
valueTThe argument to validate as equal to
other.otherTThe value to compare with
value.paramNamestringThe name of the parameter with which
valuecorresponds.
Type Parameters
TThe type of the values to compare.
Remarks
This method requires T to implement IEquatable<T> for comparison.
Exceptions
- ArgumentOutOfRangeException
valueis not equal toother.
ThrowIfNull(nint, string?)
Throws an ArgumentNullException if argument is null (IntPtr.Zero).
public static void ThrowIfNull(nint argument, string? paramName = null)
Parameters
argumentnintThe pointer argument to validate as non-null.
paramNamestringThe name of the parameter with which
argumentcorresponds.
Remarks
Validates that an nint is not zero, which represents a null pointer.
Exceptions
- ArgumentNullException
argumentis Zero.
ThrowIfNull(object?, string?)
Throws an ArgumentNullException if argument is null.
public static void ThrowIfNull(object? argument, string? paramName = null)
Parameters
argumentobjectThe reference type argument to validate as non-null.
paramNamestringThe name of the parameter with which
argumentcorresponds.
Remarks
On .NET 7.0 and above, this method uses the framework's built-in 'ArgumentNullException.ThrowIfNull(object?, string?)' method.
Exceptions
- ArgumentNullException
argumentis null.
ThrowIfNullOrEmpty(string?, string?)
Throws an exception if argument is null or empty.
public static void ThrowIfNullOrEmpty(string? argument, string? paramName = null)
Parameters
argumentstringThe string argument to validate as non-null and non-empty.
paramNamestringThe name of the parameter with which
argumentcorresponds.
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
argumentis null.- ArgumentException
argumentis 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
argumentstringThe string argument to validate.
paramNamestringThe name of the parameter with which
argumentcorresponds.
Remarks
This method performs a more stringent validation than ThrowIfNullOrEmpty(string?, string?) by also checking for whitespace-only strings.
Exceptions
- ArgumentNullException
argumentis null.- ArgumentException
argumentis 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
valuebyteThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(decimal, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(decimal value, string? paramName = null)
Parameters
valuedecimalThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(double, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(double value, string? paramName = null)
Parameters
valuedoubleThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(short, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(short value, string? paramName = null)
Parameters
valueshortThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(int, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(int value, string? paramName = null)
Parameters
valueintThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(long, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(long value, string? paramName = null)
Parameters
valuelongThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(sbyte, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(sbyte value, string? paramName = null)
Parameters
valuesbyteThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(float, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(float value, string? paramName = null)
Parameters
valuefloatThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(ushort, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(ushort value, string? paramName = null)
Parameters
valueushortThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(uint, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(uint value, string? paramName = null)
Parameters
valueuintThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.
ThrowIfZero(ulong, string?)
Throws an ArgumentOutOfRangeException if value is zero.
public static void ThrowIfZero(ulong value, string? paramName = null)
Parameters
valueulongThe argument to validate as non-zero.
paramNamestringThe name of the parameter with which
valuecorresponds.
Exceptions
- ArgumentOutOfRangeException
valueis zero.