dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(error));

Error message

throw new ArgumentNullException(nameof(error));

What it means

AsyncObservable.Throw creates a sequence that fails immediately with the supplied exception. The error itself must be a real Exception instance — a null error would make the resulting sequence's failure undefined — so the guard throws ArgumentNullException naming the error parameter.

Solutions

  1. Pass a concrete exception, e.g. new InvalidOperationException("reason")
  2. Guard the call: if (err == null) use AsyncObservable.Empty<TSource>() instead
  3. Fix the code path that produced a null Exception

Example fix

// before
var seq = AsyncObservable.Throw<int>(lastError); // null
// after
var seq = lastError != null
    ? AsyncObservable.Throw<int>(lastError)
    : AsyncObservable.Throw<int>(new InvalidOperationException("unknown failure"));
Defensive patterns

Strategy: validation

Validate before calling

if (error is null) throw new ArgumentNullException(nameof(error));
// or pick an alternative sequence:
var seq = error != null ? AsyncObservable.Throw<T>(error) : AsyncObservable.Empty<T>();

Type guard

static bool IsValidError(Exception e) => e is not null;

Try / catch

try { var seq = AsyncObservable.Throw<T>(error); }
catch (ArgumentNullException ex) when (ex.ParamName == "error") { /* substitute default exception */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.Throw<TSource>(null), often with an exception variable that is null (e.g. caught exception captured into a nullable field that was never set).

Common situations: Storing 'lastError' in a field that is null on the first failure path; mapping from an error code where no matching Exception exists; passing Exception from an API that returns null instead of throwing.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Throw.cs:15

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Reactive.Concurrency;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> Throw<TSource>(Exception error)
        {
            if (error == null)
                throw new ArgumentNullException(nameof(error));

            return Create<TSource>(observer => AsyncObserver.Throw(observer, error));
        }

        public static IAsyncObservable<TSource> Throw<TSource>(Exception error, IAsyncScheduler scheduler)
        {
            if (error == null)
                throw new ArgumentNullException(nameof(error));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return Create<TSource>(observer => AsyncObserver.Throw(observer, error, scheduler));
        }
    }

    public partial class AsyncObserver
    {
        public static ValueTask<IAsyncDisposable> Throw<TSource>(IAsyncObserver<TSource> observer, Exception error) => Throw(observer, error, ImmediateAsyncScheduler.Instance);

View on GitHub (pinned to 94b5d5ab91)