Skip to content

OnixLabs.Core.Failure T

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

Failure<T> Class

Represents a failed generic result that carries the underlying exception.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Result<T> → Failure<T>

Definition

public sealed class Failure<T> : Result<T>

Type Parameters

Parameter Description
T The type of the value that would have been carried had the operation succeeded.

Failure<T> has no public constructor. Obtain an instance via Result<T>.Failure(Exception) or the implicit conversion from Exception to Result<T>.

Remarks

Failure<T> is the failure rail of the railway-oriented Result<T> pattern. It is one of exactly two sealed derivations of the closed base class Result<T> (the other being Success<T>), so a C# 15 switch over Success<T> and Failure<T> is exhaustive without a discard arm.

Key behaviours:

  • Construction. The constructor is internal; instances can only be created by Result<T>.Failure(Exception exception) or the implicit conversion Exception → Result<T>.
  • Exception access. The Exception property exposes the captured exception directly. For safe, fluent access use base-class accessors such as GetExceptionOrDefault() and GetExceptionOrThrow().
  • Re-typing. ToTypedResult<TResult>() re-wraps the same exception in a Failure<TResult>, and ToUntypedResult() converts it to the non-generic Failure. Both are useful when propagating failures across type boundaries inside combinators.
  • Short-circuit. Every combinator on Result<T> (Select, SelectMany, Match, and their async variants) short-circuits immediately when the instance is a Failure<T>, passing the original failure through without re-evaluating any delegate.
  • Disposal. Dispose() and DisposeAsync() are no-ops for a Failure<T>; there is no value to dispose.
  • Equality. Two Failure<T> instances are equal when their Exception references are equal (reference equality via object.Equals). A Failure<T> is never equal to a Success<T>.
  • ToString. Returns Exception.Message.
  • Rethrow. Call Throw() or GetValueOrThrow() to leave the railway and rethrow the captured exception with its original stack trace preserved via ExceptionDispatchInfo.

Properties

Property Type Description
Exception Exception Gets the underlying exception representing the failed result.

Exception

public Exception Exception { get; }

Gets the underlying exception representing the failed result. Set once at construction and never null.

Methods

Method Returns Description
ToTypedResult<TResult>() Failure<TResult> Re-types this failure to carry the same exception under a different result type.
ToUntypedResult() Failure Converts this failure to a non-generic Failure carrying the same exception.

ToTypedResult<TResult>()

public Failure<TResult> ToTypedResult<TResult>()

Obtains a new Failure<TResult> containing the current Exception, re-typing a failure from one value type to another without wrapping or re-throwing.

Type Parameter Description
TResult The underlying type of the new Failure<TResult>.

Returns — A new Failure<TResult> carrying Exception.

ToUntypedResult()

public Failure ToUntypedResult()

Obtains a new non-generic Failure containing the current Exception.

Returns — A new Failure carrying Exception.

Inherited Members

Failure<T> inherits all public members of Result<T>, including:

Examples

Creating and using a failed result

using OnixLabs.Core;

// Via static factory
Result<int> a = Result<int>.Failure(new InvalidOperationException("oops"));

// Via implicit conversion from Exception
Exception ex = new FormatException("bad input");
Result<int> b = ex;

// Inspect the failure
if (b is Failure<int> f)
    Console.WriteLine(f.Exception.Message); // "bad input"

// Combinators short-circuit on failure
Result<string> c = b.Select(v => v.ToString()); // still a Failure<string>

Re-typing a failure across type boundaries

// Inside a combinator that needs to propagate a failure as a different type:
Failure<Order> orderFailure = someFailure.ToTypedResult<Order>();

// Or discard the type parameter altogether:
Failure untypedFailure = someFailure.ToUntypedResult();

Rethrowing the captured exception

Result<int> result = Result<int>.Of(() => int.Parse("abc"));

// Option 1 — leave the railway explicitly
result.Throw();

// Option 2 — extract the value (throws on failure)
int value = result.GetValueOrThrow();

// Option 3 — explicit cast (equivalent to GetValueOrThrow)
int value2 = (int)result;

See Also

ONIXLabs .NET Library


Clone this wiki locally