Class ExceptionCondition<TException>
- Namespace
- Cuemon
- Assembly
- Cuemon.Kernel.dll
Provides a fluent and generic way to setup a condition for raising an Exception.
public class ExceptionCondition<TException> where TException : Exception
Type Parameters
TExceptionThe type of the Exception.
- Inheritance
-
ExceptionCondition<TException>
Examples
The following example demonstrates how to use
using System;
using Cuemon; // for ExceptionCondition
namespace MyApp.Examples;
public class ExceptionConditionExample
{
public void Demonstrate()
{
// Throw InvalidOperationException only when a condition is true
var invoker = new ExceptionCondition<InvalidOperationException>()
.IsTrue(() => DateTime.Now.DayOfWeek == DayOfWeek.Monday)
.Create(() => new InvalidOperationException("Cannot run this operation on Mondays."));
// If today is Monday, TryThrow will throw InvalidOperationException
// If today is not Monday, TryThrow does nothing
invoker.TryThrow();
// Throw ArgumentException only when a condition is false
var falseInvoker = new ExceptionCondition<ArgumentException>()
.IsFalse(() => Environment.UserName == "admin")
.Create(() => new ArgumentException("Only admin can call this method."));
falseInvoker.TryThrow();
// Use the TesterFunc overload to pass data to the exception
TesterFunc<string, bool> tryGetValue = (out string value) =>
{
value = "cached-data";
return true; // data exists
};
var dataInvoker = new ExceptionCondition<InvalidOperationException>()
.IsTrue(tryGetValue)
.Create(data => new InvalidOperationException(
$"Value '{data}' has expired. Please refresh."));
// Since condition returns true, this throws
dataInvoker.TryThrow();
}
}
Methods
IsFalse(Func<bool>)
Indicates that the specified function delegate condition must evaluate false.
public ExceptionHandler<TException> IsFalse(Func<bool> condition)
Parameters
Returns
- ExceptionHandler<TException>
An ExceptionHandler<TException> with the specified
condition.
Exceptions
- ArgumentNullException
conditioncannot be null.
IsFalse<TResult>(TesterFunc<TResult, bool>)
Indicates that the specified function delegate condition must evaluate false.
public ExceptionHandler<TException, TResult> IsFalse<TResult>(TesterFunc<TResult, bool> condition)
Parameters
conditionTesterFunc<TResult, bool>The function delegate that determines if an Exception is thrown.
Returns
- ExceptionHandler<TException, TResult>
An ExceptionHandler<TException> with the specified
condition.
Type Parameters
TResultThe type of the out result value of the function delegate
condition.
Exceptions
- ArgumentNullException
conditioncannot be null.
IsTrue(Func<bool>)
Indicates that the specified function delegate condition must evaluate true.
public ExceptionHandler<TException> IsTrue(Func<bool> condition)
Parameters
Returns
- ExceptionHandler<TException>
An ExceptionHandler<TException> with the specified
condition.
Exceptions
- ArgumentNullException
conditioncannot be null.
IsTrue<TResult>(TesterFunc<TResult, bool>)
Indicates that the specified function delegate condition must evaluate true.
public ExceptionHandler<TException, TResult> IsTrue<TResult>(TesterFunc<TResult, bool> condition)
Parameters
conditionTesterFunc<TResult, bool>The function delegate that determines if an Exception is thrown.
Returns
- ExceptionHandler<TException, TResult>
An ExceptionHandler<TException> with the specified
condition.
Type Parameters
TResultThe type of the out result value of the function delegate
condition.
Exceptions
- ArgumentNullException
conditioncannot be null.