{"record":{"id":"8ed05c9bd464ebdf","repo":"dotnet/reactive","slug":"error-behaviorsubject","errorCode":null,"errorMessage":"error","messagePattern":"error","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Subjects/BehaviorSubject.cs","lineNumber":167,"sourceCode":"            if (os != null)\n            {\n                foreach (var o in os)\n                {\n                    o.OnCompleted();\n                }\n            }\n        }\n\n        /// <summary>\n        /// Notifies all subscribed observers about the exception.\n        /// </summary>\n        /// <param name=\"error\">The exception to send to all observers.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"error\"/> is <c>null</c>.</exception>\n        public override void OnError(Exception error)\n        {\n            if (error == null)\n            {\n                throw new ArgumentNullException(nameof(error));\n            }\n\n            IObserver<T>[]? os = null;\n\n            lock (_gate)\n            {\n                CheckDisposed();\n\n                if (!_isStopped)\n                {\n                    os = _observers.Data;\n                    _observers = ImmutableList<IObserver<T>>.Empty;\n                    _isStopped = true;\n                    _exception = error;\n                }\n            }\n\n            if (os != null)","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Subjects/BehaviorSubject.cs#L149-L185","documentation":"BehaviorSubject.OnError throws ArgumentNullException when the error parameter is null. Like all Rx subjects, it enforces the contract that OnError carries a non-null exception before forwarding it to subscribers.","triggerScenarios":"Calling BehaviorSubject<T>.OnError(null) from a source wrapper or exception-forwarding lambda.","commonSituations":"Forwarding Task.Exception from a non-faulted task; UI event handlers with null error payloads.","solutions":["Pass a real exception instance; wrap unknown failures explicitly.","Null-check the exception before calling OnError.","Prefer OnErrorResumeNext or Catch operators to convert nulls/absent errors into typed failures."],"exampleFix":"// before\nsubject.OnError(task.Exception);\n// after\nsubject.OnError(task.Exception ?? new InvalidOperationException(\"task failed without exception\"));","handlingStrategy":"validation","validationCode":"if (error != null) subject.OnError(error);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Coalesce null errors into a concrete exception before notifying.","Keep exception forwarding code free of nullable Exception sources (e.g. Task.Exception on non-faulted tasks)."],"tags":["argument-null","rx","subject"],"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"}