dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'handler')

Error message

Value cannot be null. (Parameter 'handler')

What it means

The Catch operator overload Catch<TSource,TException>(source, handler) throws ArgumentNullException with parameter name 'handler' when the exception-handler delegate is null. The delegate produces the replacement sequence when an exception of the specified type occurs, so the library rejects it eagerly at call time.

Solutions

  1. Provide an actual handler lambda, e.g. ex => Enumerable.Empty<TSource>() or a recovery sequence.
  2. Check why the handler expression evaluated to null (missing registration, failed lookup).
  3. Coalesce: source.Catch(handler ?? (_ => Enumerable.Empty<T>())).
  4. Use nullable-aware signatures with NRT enabled to catch it at compile time.

Example fix

// before
var result = source.Catch<MyException>(null);
// after
var result = source.Catch<MyException>(ex => Enumerable.Empty<int>());
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool HasHandler<TSource, TException>(Func<TException, IEnumerable<TSource>> h) where TException : Exception => h != null;

Try / catch

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

Prevention

When it happens

Trigger: Calling source.Catch(handler) where handler is a null Func<TException, IEnumerable<TSource>>, e.g. a conditional expression or method result that yielded null.

Common situations: A handler variable assigned from configuration or a registry that was not registered; refactoring removed the handler lambda; passing a null result from a factory method.

Related errors


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

Appendix: source

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

{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from
        /// calling an exception handler function in case of an error.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TException">Exception type to catch.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
        /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
        public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
            where TException : Exception
        {
            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);
        }

View on GitHub (pinned to 94b5d5ab91)