Table of Contents

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

condition bool

The condition to evaluate.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

paramName string

The name of the parameter that caused the exception.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

paramName string

The name of the parameter that caused the exception.

message string

An 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

condition bool

The condition to evaluate.

actualValue int?

The value that caused the exception.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

actualValue int?

The value that caused the exception.

paramName string

The name of the parameter that caused the exception.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

actualValue int?

The value that caused the exception.

paramName string

The name of the parameter that caused the exception.

message string

An 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

index int

The destination index to check.

length int

The destination length.

paramName string

The 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

condition bool

The condition to evaluate.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

objectName string

The name of the object that has been disposed.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

objectName string

The name of the object that has been disposed.

message string

An 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

index int

The index to check.

key object

The key associated with the index.

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

condition bool

The condition to evaluate.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

key object

The key associated with the lookup.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

key object

The key associated with the lookup.

message string

An 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

index int

The index to check.

count int

The total number of elements in the collection.

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

value int

The value to check.

paramName string

The name of the parameter that caused the exception.

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

value int

The value to check.

paramName string

The name of the parameter that caused the exception.

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

argument object

The argument to check for null.

paramName string

The 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

value object

The value to check.

defaultValue TValue

A default value of type TValue used to determine if nulls are illegal.

paramName string

The name of the parameter that caused the exception.

Type Parameters

TValue

The 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

argument T

The argument to check for null.

paramName string

The name of the parameter that caused the exception.

Type Parameters

T

The 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

condition bool

The condition to evaluate.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

objectName string

The name of the object in an invalid state.

message SwiftThrowInterpolatedStringHandler

The 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

condition bool

The condition to evaluate.

objectName string

The name of the object in an invalid state.

message string

An optional message to include in the exception.

Exceptions

InvalidOperationException

Thrown when the condition is true.