{"record":{"id":"1f19c67a3b2263a5","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-scheduler-1f19c6","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(scheduler));","messagePattern":"throw new ArgumentNullException\\(nameof\\(scheduler\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Throw.cs","lineNumber":25,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Throw<TSource>(Exception error)\n        {\n            if (error == null)\n                throw new ArgumentNullException(nameof(error));\n\n            return Create<TSource>(observer => AsyncObserver.Throw(observer, error));\n        }\n\n        public static IAsyncObservable<TSource> Throw<TSource>(Exception error, IAsyncScheduler scheduler)\n        {\n            if (error == null)\n                throw new ArgumentNullException(nameof(error));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<TSource>(observer => AsyncObserver.Throw(observer, error, scheduler));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static ValueTask<IAsyncDisposable> Throw<TSource>(IAsyncObserver<TSource> observer, Exception error) => Throw(observer, error, ImmediateAsyncScheduler.Instance);\n\n        public static ValueTask<IAsyncDisposable> Throw<TSource>(IAsyncObserver<TSource> observer, Exception error, IAsyncScheduler scheduler)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return scheduler.ScheduleAsync(async ct =>\n            {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Throw.cs#L7-L43","documentation":"The Throw operator validates that both the error to emit and the scheduler used to schedule the emission are non-null. Passing a null IAsyncScheduler means the operator cannot schedule OnErrorAsync, so it fails fast with ArgumentNullException instead of failing later inside the scheduled work. This is standard eager argument validation performed synchronously when Throw is called.","triggerScenarios":"Calling AsyncObservable.Throw<TSource>(error, scheduler) with a null scheduler argument, e.g. a scheduler variable that was never initialized or a factory method that returned null.","commonSituations":"Developers resolving the scheduler from DI/configuration where registration is missing; passing the result of a lookup that returns null; refactoring code that previously used ImmediateAsyncScheduler.Instance.","solutions":["Pass a concrete non-null IAsyncScheduler, e.g. ImmediateAsyncScheduler.Instance or a TestAsyncScheduler instance","Check the code path producing the scheduler value and fix the null source (missing DI registration, failed lookup)","If no scheduling is needed, use the Throw(error) overload that defaults to the immediate scheduler"],"exampleFix":"// before\nvar obs = AsyncObservable.Throw<int>(new Exception(\"boom\"), scheduler); // scheduler is null\n// after\nvar obs = AsyncObservable.Throw<int>(new Exception(\"boom\"), scheduler ?? ImmediateAsyncScheduler.Instance);","handlingStrategy":"validation","validationCode":"if (error == null) throw new ArgumentNullException(nameof(error));\nif (scheduler == null) throw new ArgumentNullException(nameof(scheduler));\nvar obs = AsyncObservable.Throw<int>(error, scheduler);","typeGuard":"bool IsValidScheduler(IAsyncScheduler s) => s is not null;","tryCatchPattern":"try\n{\n    var obs = AsyncObservable.Throw<int>(error, scheduler);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\")\n{\n    // fall back to immediate scheduler\n    var obs = AsyncObservable.Throw<int>(error, ImmediateAsyncScheduler.Instance);\n}","preventionTips":["Default to ImmediateAsyncScheduler.Instance when no scheduler is needed","Assert non-null scheduler at application startup / DI validation","Avoid nullable scheduler variables; resolve eagerly"],"tags":["argument-null","scheduler","validation"],"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"}