{"record":{"id":"789ebcaa8cd1a64b","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-exception-observable-creation","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'exception')","messagePattern":"Value cannot be null\\. \\(Parameter 'exception'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":612,"sourceCode":"            return s_impl.Return(value, scheduler);\n        }\n\n        #endregion\n\n        #region + Throw +\n\n        /// <summary>\n        /// Returns an observable sequence that terminates with an exception.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type used for the <see cref=\"IObservable{T}\"/> type parameter of the resulting sequence.</typeparam>\n        /// <param name=\"exception\">Exception object used for the sequence's termination.</param>\n        /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"exception\"/> is null.</exception>\n        public static IObservable<TResult> Throw<TResult>(Exception exception)\n        {\n            if (exception == null)\n            {\n                throw new ArgumentNullException(nameof(exception));\n            }\n\n            return s_impl.Throw<TResult>(exception);\n        }\n\n        /// <summary>\n        /// Returns an observable sequence that terminates with an exception.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type used for the <see cref=\"IObservable{T}\"/> type parameter of the resulting sequence.</typeparam>\n        /// <param name=\"exception\">Exception object used for the sequence's termination.</param>\n        /// <param name=\"witness\">Object solely used to infer the type of the <typeparamref name=\"TResult\"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>\n        /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"exception\"/> is null.</exception>\n#pragma warning disable IDE0060 // (Remove unused parameter.) Required for type inference\n        public static IObservable<TResult> Throw<TResult>(Exception exception, TResult witness)\n#pragma warning restore IDE0060\n        {\n            if (exception == null)","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L594-L630","documentation":"Observable.Throw<TResult>(exception) creates a sequence that terminates with the given exception, and the library requires a non-null exception instance — it throws ArgumentNullException naming 'exception'. A null exception cannot be signaled through OnError, so the factory rejects it up front.","triggerScenarios":"Calling Observable.Throw<TResult>(Exception exception) with exception == null, e.g. Observable.Throw<Unit>(caughtException) where caughtException was never assigned, or Observable.Throw<int>(ex.InnerException) when there is no inner exception.","commonSituations":"Reading InnerException when the caught exception has none; an error factory/exception-provider returning null; conditional code paths where the exception variable is only populated on failure; deserialized exception fields that are null.","solutions":["Pass a concrete exception instance, e.g. Observable.Throw<T>(new InvalidOperationException(\"reason\")).","Fallback when the source exception is null: use a default exception like new Exception(\"Unknown error\").","Guard before calling: if (ex == null) throw new ArgumentNullException(nameof(ex)); to fail with your own context.","If you only need to terminate the sequence, consider Observable.Empty<T>() or generating a meaningful error instead."],"exampleFix":"// before\nvar xs = Observable.Throw<int>(ex.InnerException); // null when ex has no inner\n// after\nvar cause = ex.InnerException ?? ex ?? new Exception(\"Unknown failure\");\nvar xs = Observable.Throw<int>(cause);","handlingStrategy":"validation","validationCode":"if (exception == null)\n    throw new ArgumentException(\"A non-null exception is required.\", nameof(exception));\nvar xs = Observable.Throw<T>(exception);","typeGuard":"static bool HasError(Exception ex) => ex is not null;","tryCatchPattern":"try { var xs = Observable.Throw<T>(ex); }\ncatch (ArgumentNullException ex2) when (ex2.ParamName == \"exception\")\n{ var xs = Observable.Throw<T>(new Exception(\"Unknown error\")); }","preventionTips":["Never forward ex.InnerException directly — it can be null; coalesce with ?? .","Centralize error-sequence creation in one guarded helper.","If the exception is null because there was no failure, emit Empty/Return instead."],"tags":["rx","null-argument","exception","throw"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}