Class SwiftThrowHelper
- Namespace
- SwiftCollections.Diagnostics
- Assembly
- SwiftCollections.dll
Provides allocation-conscious guard helpers for throwing common exceptions.
public static class SwiftThrowHelper
- Inheritance
-
SwiftThrowHelper
- Inherited Members
Remarks
Validation methods keep the success path small and route exception creation through no-inline throw helpers. Interpolated custom messages are condition-gated so formatted expressions are not evaluated unless the guard throws.
Methods
ThrowIfArgument(bool, SwiftThrowInterpolatedStringHandler)
Throws an ArgumentException with a lazily formatted message if the specified condition is true.
public static void ThrowIfArgument(bool condition, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ArgumentException
Thrown when the condition is true.
ThrowIfArgument(bool, string?, SwiftThrowInterpolatedStringHandler)
Throws an ArgumentException with a lazily formatted message if the specified condition is true.
public static void ThrowIfArgument(bool condition, string? paramName, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
paramNamestringThe name of the parameter that caused the exception.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ArgumentException
Thrown when the condition is true.
ThrowIfArgument(bool, string?, string?)
Throws an ArgumentException if the specified condition is true.
public static void ThrowIfArgument(bool condition, string? paramName = null, string? message = null)
Parameters
conditionboolThe condition to evaluate.
paramNamestringThe name of the parameter that caused the exception.
messagestringAn optional message to include in the exception.
Exceptions
- ArgumentException
Thrown when the condition is true.
ThrowIfArgumentOutOfRange(bool, int?, SwiftThrowInterpolatedStringHandler)
Throws an ArgumentOutOfRangeException with a lazily formatted message if the specified condition is true.
public static void ThrowIfArgumentOutOfRange(bool condition, int? actualValue, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
actualValueint?The value that caused the exception.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ArgumentOutOfRangeException
Thrown when the condition is true.
ThrowIfArgumentOutOfRange(bool, int?, string?, SwiftThrowInterpolatedStringHandler)
Throws an ArgumentOutOfRangeException with a lazily formatted message if the specified condition is true.
public static void ThrowIfArgumentOutOfRange(bool condition, int? actualValue, string? paramName, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
actualValueint?The value that caused the exception.
paramNamestringThe name of the parameter that caused the exception.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ArgumentOutOfRangeException
Thrown when the condition is true.
ThrowIfArgumentOutOfRange(bool, int?, string?, string?)
Throws an ArgumentOutOfRangeException if the specified condition is true.
public static void ThrowIfArgumentOutOfRange(bool condition, int? actualValue, string? paramName = null, string? message = null)
Parameters
conditionboolThe condition to evaluate.
actualValueint?The value that caused the exception.
paramNamestringThe name of the parameter that caused the exception.
messagestringAn optional message to include in the exception.
Exceptions
- ArgumentOutOfRangeException
Thrown when the condition is true.
ThrowIfArrayIndexInvalid(int, int, string?)
Throws an ArgumentOutOfRangeException if a copy destination index is outside [0, length].
public static void ThrowIfArrayIndexInvalid(int index, int length, string? paramName = null)
Parameters
indexintThe destination index to check.
lengthintThe destination length.
paramNamestringThe name of the parameter that caused the exception.
Exceptions
- ArgumentOutOfRangeException
Thrown when the index is outside [0, length].
ThrowIfDisposed(bool, SwiftThrowInterpolatedStringHandler)
Throws an ObjectDisposedException with a lazily formatted message if the specified condition is true.
public static void ThrowIfDisposed(bool condition, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ObjectDisposedException
Thrown when the condition is true.
ThrowIfDisposed(bool, string?, SwiftThrowInterpolatedStringHandler)
Throws an ObjectDisposedException with a lazily formatted message if the specified condition is true.
public static void ThrowIfDisposed(bool condition, string? objectName, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
objectNamestringThe name of the object that has been disposed.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- ObjectDisposedException
Thrown when the condition is true.
ThrowIfDisposed(bool, string?, string?)
Throws an ObjectDisposedException if the specified condition is true.
public static void ThrowIfDisposed(bool condition, string? objectName = null, string? message = null)
Parameters
conditionboolThe condition to evaluate.
objectNamestringThe name of the object that has been disposed.
messagestringAn optional message to include in the exception.
Exceptions
- ObjectDisposedException
Thrown when the condition is true.
ThrowIfKeyInvalid(int, object?)
Throws a KeyNotFoundException if the specified index is negative.
public static void ThrowIfKeyInvalid(int index, object? key = null)
Parameters
Exceptions
- KeyNotFoundException
Thrown when the index is negative.
ThrowIfKeyNotFound(bool, SwiftThrowInterpolatedStringHandler)
Throws a KeyNotFoundException with a lazily formatted message if the specified condition is true.
public static void ThrowIfKeyNotFound(bool condition, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- KeyNotFoundException
Thrown when the condition is true.
ThrowIfKeyNotFound(bool, object?, SwiftThrowInterpolatedStringHandler)
Throws a KeyNotFoundException with a lazily formatted message if the specified condition is true.
public static void ThrowIfKeyNotFound(bool condition, object? key, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
keyobjectThe key associated with the lookup.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- KeyNotFoundException
Thrown when the condition is true.
ThrowIfKeyNotFound(bool, object?, string?)
Throws a KeyNotFoundException if the specified condition is true.
public static void ThrowIfKeyNotFound(bool condition, object? key = null, string? message = null)
Parameters
conditionboolThe condition to evaluate.
keyobjectThe key associated with the lookup.
messagestringAn optional message to include in the exception.
Exceptions
- KeyNotFoundException
Thrown when the condition is true.
ThrowIfListIndexInvalid(int, int)
Throws an IndexOutOfRangeException if the specified index is outside the valid range defined by count.
public static void ThrowIfListIndexInvalid(int index, int count)
Parameters
Exceptions
- IndexOutOfRangeException
Thrown when the index is outside the valid range.
ThrowIfNegative(int, string?)
Throws an ArgumentOutOfRangeException if the specified value is negative.
public static void ThrowIfNegative(int value, string? paramName = null)
Parameters
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is negative.
ThrowIfNegativeOrZero(int, string?)
Throws an ArgumentOutOfRangeException if the specified value is negative or zero.
public static void ThrowIfNegativeOrZero(int value, string? paramName = null)
Parameters
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is negative or zero.
ThrowIfNull(object?, string?)
Throws an ArgumentNullException if the provided argument is null.
public static void ThrowIfNull(object? argument, string? paramName = null)
Parameters
argumentobjectThe argument to check for null.
paramNamestringThe name of the parameter that caused the exception.
Exceptions
- ArgumentNullException
Thrown when the argument is null.
ThrowIfNullAndNullsAreIllegal<TValue>(object?, TValue?, string?)
Throws an ArgumentNullException if the specified value is null and nulls are not legal for TValue.
public static void ThrowIfNullAndNullsAreIllegal<TValue>(object? value, TValue? defaultValue, string? paramName = null)
Parameters
valueobjectThe value to check.
defaultValueTValueA default value of type
TValueused to determine if nulls are illegal.paramNamestringThe name of the parameter that caused the exception.
Type Parameters
TValueThe value type used to determine whether null is legal.
Exceptions
- ArgumentNullException
Thrown when the value is null and nulls are illegal.
ThrowIfNullGeneric<T>(T, string?)
Throws an ArgumentNullException if the provided generic argument is null.
public static void ThrowIfNullGeneric<T>(T argument, string? paramName = null)
Parameters
argumentTThe argument to check for null.
paramNamestringThe name of the parameter that caused the exception.
Type Parameters
TThe type of the argument to check.
Exceptions
- ArgumentNullException
Thrown when the argument is null.
ThrowIfTrue(bool, SwiftThrowInterpolatedStringHandler)
Throws an InvalidOperationException with a lazily formatted message if the specified condition is true.
public static void ThrowIfTrue(bool condition, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- InvalidOperationException
Thrown when the condition is true.
ThrowIfTrue(bool, string?, SwiftThrowInterpolatedStringHandler)
Throws an InvalidOperationException with a lazily formatted message if the specified condition is true.
public static void ThrowIfTrue(bool condition, string? objectName, SwiftThrowInterpolatedStringHandler message)
Parameters
conditionboolThe condition to evaluate.
objectNamestringThe name of the object in an invalid state.
messageSwiftThrowInterpolatedStringHandlerThe interpolated exception message.
Exceptions
- InvalidOperationException
Thrown when the condition is true.
ThrowIfTrue(bool, string?, string?)
Throws an InvalidOperationException if the specified condition is true.
public static void ThrowIfTrue(bool condition, string? objectName = null, string? message = null)
Parameters
conditionboolThe condition to evaluate.
objectNamestringThe name of the object in an invalid state.
messagestringAn optional message to include in the exception.
Exceptions
- InvalidOperationException
Thrown when the condition is true.