Class ExceptionHandler<TException, TResult>
- Namespace
- Cuemon
- Assembly
- Cuemon.Kernel.dll
Provides a generic way to handle an Exception.
public class ExceptionHandler<TException, TResult> where TException : Exception
Type Parameters
TExceptionThe type of the Exception.
TResultThe type of the out result value of a TesterFunc<TResult, TSuccess>.
- Inheritance
-
ExceptionHandler<TException, TResult>
Examples
The following example demonstrates how to use the generic class in the fluent exception-triggering chain with an out-value (tester) condition.
using System;
using Cuemon;
namespace Cuemon.DocfxExamples;
public sealed class ExceptionHandlerOfTResultExample
{
public static void Run()
{
TesterFunc<int, bool> parsePort = (out int value) => int.TryParse("70000", out value);
ExceptionHandler<ArgumentOutOfRangeException, int> handler =
new ExceptionCondition<ArgumentOutOfRangeException>().IsTrue(parsePort);
var invoker = handler.Create(port =>
new ArgumentOutOfRangeException(nameof(port), port, "Ports must be between 0 and 65535."));
try
{
invoker.TryThrow();
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine($"Rejected port: {ex.ActualValue}");
}
}
}
Methods
Create(Func<TResult, TException>)
Specifies the function delegate that determines the Exception to be thrown.
public ExceptionInvoker<TException, TResult> Create(Func<TResult, TException> handler)
Parameters
Returns
- ExceptionInvoker<TException, TResult>
An ExceptionInvoker<TException> with the specified
handler.
Exceptions
- ArgumentNullException
handlercannot be null.