dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'sources')

Error message

Value cannot be null. (Parameter 'sources')

What it means

The params / IEnumerable overload Catch(sources) validates the sources collection itself before enumerating it. A null array or null IEnumerable<IAsyncObservable<TSource>> gives no sources to try, so the operator throws ArgumentNullException named 'sources'.

Solutions

  1. Ensure the collection is non-null before calling; use an empty array to mean 'no sources'
  2. Coalesce with Array.Empty<IAsyncObservable<TSource>>() or Enumerable.Empty<TSource>()
  3. Inspect the expression producing the collection for null-returning paths

Example fix

// before
var result = AsyncObservable.Catch(sourcesOrNull);
// after
var result = AsyncObservable.Catch(sourcesOrNull ?? Array.Empty<IAsyncObservable<int>>());
Defensive patterns

Strategy: validation

Validate before calling

if (sources is null) throw new ArgumentNullException(nameof(sources));

Type guard

bool HasSources<T>(IEnumerable<IAsyncObservable<T>> sources) => sources != null;

Try / catch

try { var result = AsyncObservable.Catch(sources); } catch (ArgumentNullException ex) { /* ex.ParamName == "sources"; use empty array */ }

Prevention

When it happens

Trigger: Calling Catch() with a null array, or passing a null IEnumerable built from a filter/lookup (e.g. sources?.ToArray() returning null), or invoking the params overload with a single null-cast argument like Catch((IAsyncObservable<TSource>[])null).

Common situations: Dynamic lists of failover sources that turn out null, LINQ chains assigned to a nullable variable, or params arrays built conditionally and never initialized.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Catch.cs:82

            return Create(
                first,
                second,
                static async (first, second, observer) =>
                {
                    var (sink, inner) = AsyncObserver.Catch(observer, second);

                    var subscription = await first.SubscribeSafeAsync(sink).ConfigureAwait(false);

                    return StableCompositeAsyncDisposable.Create(subscription, inner);
                });
        }

        public static IAsyncObservable<TSource> Catch<TSource>(params IAsyncObservable<TSource>[] sources) => Catch((IEnumerable<IAsyncObservable<TSource>>)sources);

        public static IAsyncObservable<TSource> Catch<TSource>(this IEnumerable<IAsyncObservable<TSource>> sources)
        {
            if (sources == null)
                throw new ArgumentNullException(nameof(sources));

            return Create<TSource>(async observer =>
            {
                var enumerator = sources.GetEnumerator();

                if (!enumerator.MoveNext())
                {
                    return AsyncDisposable.Nop; // REVIEW: Is Never behavior right here?
                }

                var source = enumerator.Current;

                var (sink, inner) = AsyncObserver.Catch(observer, enumerator);

                var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);

                return StableCompositeAsyncDisposable.Create(subscription, inner);
            });

View on GitHub (pinned to 94b5d5ab91)