Result Pattern vs Exceptions: Verified RyuJIT Measurements and Contracts
Why exceptions for domain control flow degrade throughput and how struct-based Result<T> in EricksonLopez.Result achieves 0 B allocations in BenchmarkDotNet.
In enterprise C#, few architectural shortcuts create more hidden latency spikes and GC pressure than throwing exceptions for expected business outcomes.
The Flawed Tradition: Exception-Driven Flow
Consider a domain service that uses exceptions for ordinary validation and state checks:
public Order SettleOrder(Guid orderId, decimal amount){ var order = _repository.Find(orderId); if (order is null) throw new OrderNotFoundException(orderId);
if (order.IsAlreadySettled) throw new OrderAlreadySettledException(orderId);
order.Settle(amount); return order;}When an exception is thrown in .NET:
- The CLR captures and materializes stack trace metadata, allocating the exception instance and related state on the managed heap.
- Stack unwinding walks frames until a matching
catchblock handles the failure. - Exceptions bypass the method’s static return signature, hiding representable domain errors from callers.
Verified BenchmarkDotNet RyuJIT Measurements
Measured using BenchmarkDotNet on .NET 8.0.29 X64 RyuJIT x86-64-v4 in dotnet-result (CompetitorBenchmarks and CompetitorExceptionBenchmarks, plus ResultConstructionBenchmarks on .NET 10.0.1 RyuJIT):
| Scenario (RyuJIT) | Runtime | Mean Time | Allocated Memory |
|---|---|---|---|
EricksonLopez_Success (Result) / EricksonLopez_SuccessValue (Result<int>) |
.NET 8.0.29 RyuJIT |
0.000 ns |
0 B |
EricksonLopez_Failure (sentinel Error) |
.NET 8.0.29 RyuJIT |
0.772 ns |
0 B |
EricksonLopez_FailureValue (Result<int>) |
.NET 8.0.29 RyuJIT |
0.7886 ns |
0 B |
EricksonLopez_Exception (throw + catch) |
.NET 8.0.29 RyuJIT |
2,441.0 ns |
96 B |
ResultConstructionBenchmarks.Success_Int |
.NET 10.0.1 RyuJIT |
0.0889 ns |
0 B |
(Note: all verified BenchmarkDotNet reports in the workspace execute under X64 RyuJIT; zero NativeAOT BenchmarkDotNet artifacts have been recorded.)
Returning a readonly struct Result failure with a cached Error executes in sub-nanosecond time with 0 B heap allocation, whereas throwing and catching a lightweight exception costs 2,441 ns and 96 B per failure.
Explicit Return Contracts with EricksonLopez.Result
Under EricksonLopez.Result, expected domain outcomes—both Result (void-equivalent) and Result<TValue>—are encoded directly in the return type:
using EricksonLopez.Result;
public Result<Order> SettleOrder(OrderId orderId, Money amount){ Order? order = _repository.GetById(orderId); if (order is null) { return Result.Failure<Order>(OrderErrors.NotFound(orderId)); }
Result settleResult = order.Settle(amount); if (settleResult.IsFailure) { return Result.Failure<Order>(settleResult.Error); }
return Result.Success(order);}Exceptions remain reserved for truly exceptional failures (corrupted storage, severed sockets, unrecoverable I/O faults). Expected business rejections belong in explicit Result contracts.