dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'sources')

Error message

Value cannot be null. (Parameter 'sources')

What it means

The Catch overload taking a sequence of sequences, Catch<TSource>(IEnumerable<IEnumerable<TSource>> sources), throws ArgumentNullException with parameter name 'sources' when the outer sequence is null. The library requires a (possibly empty) container of sequences; null is rejected eagerly at call time.

Solutions

  1. Pass a non-null enumerable; use an empty sequence if there are no candidates.
  2. Coalesce at the call site: (sources ?? Enumerable.Empty<IEnumerable<T>>()).Catch().
  3. Fix the producer to return Enumerable.Empty<IEnumerable<T>>() instead of null.
  4. Enable nullable reference types to catch it at compile time.

Example fix

// before
var result = ((List<IEnumerable<int>>)null).Catch();
// after
var result = (sources ?? Enumerable.Empty<IEnumerable<int>>()).Catch();
Defensive patterns

Strategy: validation

Validate before calling

if (sources is null)
    throw new ArgumentNullException(nameof(sources));
// or coalesce:
sources ??= Enumerable.Empty<IEnumerable<TSource>>();

Type guard

static bool IsUsableSources<TSource>(IEnumerable<IEnumerable<TSource>> s) => s != null;

Try / catch

try
{
    var result = sources.Catch();
}
catch (ArgumentNullException ex) when (ex.ParamName == "sources")
{
    var result = Enumerable.Empty<TSource>();
}

Prevention

When it happens

Trigger: Calling Catch<TSource>(sources) with a null outer enumerable, e.g. a list of retry attempts built elsewhere that was never assigned.

Common situations: A factory method returning null instead of an empty list; LINQ result of a nullable source without coalescing; deserialization producing null for an empty collection.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Catch.cs:40

        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));

            return CatchCore(source, handler);
        }

        /// <summary>
        /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="sources">Source sequences.</param>
        /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
        public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
        {
            if (sources == null)
                throw new ArgumentNullException(nameof(sources));

            return CatchCore(sources);
        }

        /// <summary>
        /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="sources">Source sequences.</param>
        /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
        public static IEnumerable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
        {
            if (sources == null)
                throw new ArgumentNullException(nameof(sources));

            return CatchCore(sources);
        }

View on GitHub (pinned to 94b5d5ab91)