dotnet/reactive · error · ArgumentNullException

error

Error message

error

What it means

Notification.CreateOnError<T>(Exception) throws ArgumentNullException when the error argument is null. An OnError notification must carry a non-null Exception, since observers receiving OnError expect a concrete exception instance. Thrown synchronously at factory time.

Solutions

  1. Pass an actual exception instance; create one if none exists, e.g. new Exception("unknown error").
  2. Skip creating the notification and use CreateOnCompleted<T>() if there is genuinely no error.
  3. Null-check the exception before calling CreateOnError and fall back to a synthesized exception.

Example fix

// before
var n = Notification.CreateOnError<int>(maybeEx); // maybeEx may be null
// after
var n = Notification.CreateOnError<int>(maybeEx ?? new Exception("unknown error"));
Defensive patterns

Strategy: validation

Validate before calling

if (error == null) error = new Exception("unknown error");
var n = Notification.CreateOnError<int>(error);

Type guard

bool HasError(Exception e) => e != null;

Try / catch

try { var n = Notification.CreateOnError<int>(error); }
catch (ArgumentNullException ex) when (ex.ParamName == "error") { var n = Notification.CreateOnError<int>(new Exception("unknown error")); }

Prevention

When it happens

Trigger: Calling Notification.CreateOnError<T>(ex) where ex is null, e.g. from a catch that captured no exception or a null exception variable/property.

Common situations: Rethrow-style code paths where the exception object was never captured; APIs that return a null Exception on failure then feed it to CreateOnError.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Notification.cs:703

        /// <param name="value">The value contained in the notification.</param>
        /// <returns>The OnNext notification containing the value.</returns>
        public static Notification<T> CreateOnNext<T>(T value)
        {
            return new Notification<T>.OnNextNotification(value);
        }

        /// <summary>
        /// Creates an object that represents an OnError notification to an observer.
        /// </summary>
        /// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
        /// <param name="error">The exception contained in the notification.</param>
        /// <returns>The OnError notification containing the exception.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception>
        public static Notification<T> CreateOnError<T>(Exception error)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            return new Notification<T>.OnErrorNotification(error);
        }

        /// <summary>
        /// Creates an object that represents an OnCompleted notification to an observer.
        /// </summary>
        /// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
        /// <returns>The OnCompleted notification.</returns>
        public static Notification<T> CreateOnCompleted<T>()
        {
            return Notification<T>.OnCompletedNotification.Instance;
        }
    }
}

#pragma warning restore CS0659

View on GitHub (pinned to 94b5d5ab91)