<PackageReference Include="System.Reactive" Version="4.2.0" />

ExceptionHelper

static class ExceptionHelper
Utility methods to handle lock-free combining of Exceptions as well as hosting a terminal-exception indicator for lock-free termination support.
public static Exception Terminated { get; }

The singleton instance of the exception indicating a terminal state, DO NOT LEAK or signal this via OnError!

public static Exception Terminate(ref Exception field)

Atomically swaps in the Terminated exception into the field and returns the previous exception in that field (which could be the Terminated instance too).

public static bool TryAddException(ref Exception field, Exception ex)

Atomically sets the field to the given new exception or combines it with any pre-existing exception as a new AggregateException unless the field contains the Terminated instance.

public static bool TrySetException(ref Exception field, Exception ex)

Tries to atomically set the Exception on the given field if it is still null.