Skip to content

OnixLabs.Core.Failure

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

Failure Class

Represents a failed, non-generic result that carries the underlying exception.

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

Definition

public sealed class Failure : Result

Failure has no public constructor; obtain an instance via Result.Failure(Exception) or the implicit conversion from Exception.

Failure is one of exactly two sealed derivations of Result. The other is Success. Because Result is a closed class, a C# 15 switch over Success and Failure is exhaustive without a discard arm.

Remarks

Failure represents the outcome of an operation that threw an exception. Rather than propagating the exception up the call stack, Result.Of and Result.OfAsync catch it and wrap it in a Failure, making the error an explicit, inspectable value.

Key behaviours:

  • Short-circuiting. Once a Result is a Failure, every combinator — Select, SelectMany, and their async variants — passes the Failure through without executing its delegate. The captured exception is never lost or re-evaluated.
  • Equality. Two Failure instances are equal when their Exception references are the same object. Result.GetHashCode returns Exception.GetHashCode().
  • ToString. Result.ToString returns Exception.Message for a Failure.
  • Implicit conversion from Exception. Any Exception is implicitly convertible to a Failure via implicit operator Result(Exception), which is why combinator catch blocks can write return exception;.
  • Re-throwing. Result.Throw rethrows the captured exception with its original stack trace preserved via ExceptionDispatchInfo.

Failure inherits the full combinator and exception-accessor API from Result. See the Result page for the complete inherited member list.

To promote an untyped Failure to a typed Failure<TResult>, use ToTypedResult<TResult>().

Examples

Obtaining a Failure

using OnixLabs.Core;

// Via the factory method.
Failure failure = Result.Failure(new InvalidOperationException("Something went wrong."));

// Via Result.Of — returns Failure when the action throws.
Result result = Result.Of(() => throw new IOException("Disk full."));
Console.WriteLine(result.IsFailure); // True

// Via implicit conversion from Exception.
Result implicit = new ArgumentNullException(nameof(implicit));
Console.WriteLine(implicit.IsFailure); // True

Inspecting the captured exception

using OnixLabs.Core;

Result result = Result.Of(() => throw new TimeoutException("Timed out."));

if (result is Failure f)
    Console.WriteLine(f.Exception.Message); // Timed out.

// Or via the base-class accessor:
Exception? ex = result.GetExceptionOrDefault();
Console.WriteLine(ex?.Message); // Timed out.

Promoting to a typed Failure

using OnixLabs.Core;

Failure untyped = Result.Failure(new Exception("Oops."));
Failure<int> typed = untyped.ToTypedResult<int>();
Console.WriteLine(typed.IsFailure); // True
Console.WriteLine(typed.Exception.Message); // Oops.

Properties

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

Exception

public Exception Exception { get; }

Gets the underlying exception that was captured when this Failure was created. This is the same Exception reference that Result.GetExceptionOrDefault(), Result.GetExceptionOrThrow(), and Result.Throw() operate on.

Methods

Method Returns Description
ToTypedResult<TResult>() Failure<TResult> Promotes this failure to a typed failure carrying the same exception.

ToTypedResult<TResult>()

public Failure<TResult> ToTypedResult<TResult>()

Obtains a new Failure<TResult> containing the current Exception, promoting an untyped failure to a typed one. This is the bridge from the non-generic result model to the generic Result<T> model.

Returns — A new Failure<TResult> carrying Exception.

See Also

ONIXLabs .NET Library


Clone this wiki locally