dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'exception')

Error message

Value cannot be null. (Parameter 'exception')

What it means

Observable.Throw<TResult>(exception) creates a sequence that terminates with the given exception, and the library requires a non-null exception instance — it throws ArgumentNullException naming 'exception'. A null exception cannot be signaled through OnError, so the factory rejects it up front.

Solutions

  1. Pass a concrete exception instance, e.g. Observable.Throw<T>(new InvalidOperationException("reason")).
  2. Fallback when the source exception is null: use a default exception like new Exception("Unknown error").
  3. Guard before calling: if (ex == null) throw new ArgumentNullException(nameof(ex)); to fail with your own context.
  4. If you only need to terminate the sequence, consider Observable.Empty<T>() or generating a meaningful error instead.

Example fix

// before
var xs = Observable.Throw<int>(ex.InnerException); // null when ex has no inner
// after
var cause = ex.InnerException ?? ex ?? new Exception("Unknown failure");
var xs = Observable.Throw<int>(cause);
Defensive patterns

Strategy: validation

Validate before calling

if (exception == null)
    throw new ArgumentException("A non-null exception is required.", nameof(exception));
var xs = Observable.Throw<T>(exception);

Type guard

static bool HasError(Exception ex) => ex is not null;

Try / catch

try { var xs = Observable.Throw<T>(ex); }
catch (ArgumentNullException ex2) when (ex2.ParamName == "exception")
{ var xs = Observable.Throw<T>(new Exception("Unknown error")); }

Prevention

When it happens

Trigger: Calling Observable.Throw<TResult>(Exception exception) with exception == null, e.g. Observable.Throw<Unit>(caughtException) where caughtException was never assigned, or Observable.Throw<int>(ex.InnerException) when there is no inner exception.

Common situations: Reading InnerException when the caught exception has none; an error factory/exception-provider returning null; conditional code paths where the exception variable is only populated on failure; deserialized exception fields that are null.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/789ebcaa8cd1a64b. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:612

            return s_impl.Return(value, scheduler);
        }

        #endregion

        #region + Throw +

        /// <summary>
        /// Returns an observable sequence that terminates with an exception.
        /// </summary>
        /// <typeparam name="TResult">The type used for the <see cref="IObservable{T}"/> type parameter of the resulting sequence.</typeparam>
        /// <param name="exception">Exception object used for the sequence's termination.</param>
        /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
        public static IObservable<TResult> Throw<TResult>(Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            return s_impl.Throw<TResult>(exception);
        }

        /// <summary>
        /// Returns an observable sequence that terminates with an exception.
        /// </summary>
        /// <typeparam name="TResult">The type used for the <see cref="IObservable{T}"/> type parameter of the resulting sequence.</typeparam>
        /// <param name="exception">Exception object used for the sequence's termination.</param>
        /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
        /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
#pragma warning disable IDE0060 // (Remove unused parameter.) Required for type inference
        public static IObservable<TResult> Throw<TResult>(Exception exception, TResult witness)
#pragma warning restore IDE0060
        {
            if (exception == null)

View on GitHub (pinned to 94b5d5ab91)