{"record":{"id":"e7ef8a884c4a57a9","repo":"dotnet/reactive","slug":"exception","errorCode":null,"errorMessage":"exception","messagePattern":"exception","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Runtime/CompilerServices/TaskObservableMethodBuilder.cs","lineNumber":103,"sourceCode":"                _inner = new TaskObservable(result);\n            }\n            else\n            {\n                _inner.SetResult(result);\n            }\n        }\n\n        /// <summary>\n        /// Marks the observable as failed and binds the specified exception to the observable sequence.\n        /// </summary>\n        /// <param name=\"exception\">The exception to bind to the observable sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"exception\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"InvalidOperationException\">The observable has already completed.</exception>\n        public void SetException(Exception exception)\n        {\n            if (exception == null)\n            {\n                throw new ArgumentNullException(nameof(exception));\n            }\n\n            if (_inner == null)\n            {\n                _inner = new TaskObservable(exception);\n            }\n            else\n            {\n                _inner.SetException(exception);\n            }\n        }\n\n        /// <summary>\n        /// Gets the observable sequence for this builder.\n        /// </summary>\n        public ITaskObservable<T> Task => _inner ??= new TaskObservable();\n\n        /// <summary>","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Runtime/CompilerServices/TaskObservableMethodBuilder.cs#L85-L121","documentation":"SetException requires a non-null Exception; passing null throws ArgumentNullException. It also throws if the observable already completed (the SetResult path already ran), because a task-like observable can only complete once.","triggerScenarios":"Calling SetException(null), or calling SetException after SetResult/SetException/OnCompleted already completed the observable.","commonSituations":"Bridging code that forwards exceptions from a task whose Exception property is null on success; double-completion race in custom awaitable observables.","solutions":["Pass the actual caught exception; never forward null (e.g. check Task.IsFaulted before reading Exception).","Ensure SetException is invoked exactly once, guarded by completion flags or try/finally semantics.","Guard completion with an Interlocked.CompareExchange flag in bridging code."],"exampleFix":"// before\nbuilder.SetException(task.Exception); // may be null on success\n// after\nif (task.IsFaulted && task.Exception != null) builder.SetException(task.Exception);","handlingStrategy":"validation","validationCode":"if (exception == null) throw new ArgumentNullException(nameof(exception));\nif (completed) throw new InvalidOperationException(\"observable already completed\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only forward exceptions from faulted tasks (check IsFaulted first).","Track completion with a flag so SetException/SetResult are mutually exclusive and called once."],"tags":["argument-null","async","completion-state"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}