Class Throw
- Namespace
- PatternKit.Common
- Assembly
- PatternKit.Core.dll
Provides centralized guard and throw helpers for common error scenarios in PatternKit.
public static class Throw
- Inheritance
-
Throw
- Inherited Members
Examples
// Example usage in a strategy executor:
public TOut Execute(in TIn input)
{
foreach (var (pred, handler) in _pairs)
if (pred(in input))
return handler(in input);
// No predicate matched:
return Throw.NoStrategyMatched<TOut>();
}
Remarks
These helpers are intended for use in hot paths where throwing is exceptional but must terminate execution predictably.
Typical usage is within strategy engines or fluent builders when no handler, predicate, or branch matches and no default was configured.
Methods
ArgumentNullWhenNull(object?)
public static void ArgumentNullWhenNull(object? arg = null)
Parameters
argobject
IfNegative(int, string?)
Throws an ArgumentOutOfRangeException if value is negative.
public static void IfNegative(int value, string? name = null)
Parameters
valueintThe value to check.
namestringThe name of the parameter being checked (automatically provided).
Exceptions
- ArgumentOutOfRangeException
Thrown if
valueis negative.
IfNegativeOrZero(int, string?)
Throws an ArgumentOutOfRangeException if value is zero or negative.
public static void IfNegativeOrZero(int value, string? name = null)
Parameters
valueintThe value to check.
namestringThe name of the parameter being checked (automatically provided).
Exceptions
- ArgumentOutOfRangeException
Thrown if
valueis zero or negative.
NoStrategyMatched()
Throws an InvalidOperationException indicating that no strategy branch matched and no default result was provided.
public static void NoStrategyMatched()
Remarks
Use this overload when the method return type is void or when you do not need to return a value:
if (!_handlers.Any()) Throw.NoStrategyMatched();
Exceptions
- InvalidOperationException
Always thrown with the message
"No strategy matched and no default provided.".
NoStrategyMatched<T>()
Throws an InvalidOperationException indicating that no strategy branch matched and no default result was provided.
public static T NoStrategyMatched<T>()
Returns
- T
This method never returns. The
Treturn type exists solely so it can be used in expression contexts requiring a value.
Type Parameters
TThe expected return type of the calling method.
Remarks
Use this overload when the caller must syntactically return a T value:
return Throw.NoStrategyMatched<TOut>();
Exceptions
- InvalidOperationException
Always thrown with the message
"No strategy matched and no default provided."