{"record":{"id":"5e36b6c4dbee7cc6","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-error","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(error));","messagePattern":"throw new ArgumentNullException\\(nameof\\(error\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Throw.cs","lineNumber":15,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\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);","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Throw.cs#L1-L33","documentation":"AsyncObservable.Throw creates a sequence that fails immediately with the supplied exception. The error itself must be a real Exception instance — a null error would make the resulting sequence's failure undefined — so the guard throws ArgumentNullException naming the error parameter.","triggerScenarios":"Calling AsyncObservable.Throw<TSource>(null), often with an exception variable that is null (e.g. caught exception captured into a nullable field that was never set).","commonSituations":"Storing 'lastError' in a field that is null on the first failure path; mapping from an error code where no matching Exception exists; passing Exception from an API that returns null instead of throwing.","solutions":["Pass a concrete exception, e.g. new InvalidOperationException(\"reason\")","Guard the call: if (err == null) use AsyncObservable.Empty<TSource>() instead","Fix the code path that produced a null Exception"],"exampleFix":"// before\nvar seq = AsyncObservable.Throw<int>(lastError); // null\n// after\nvar seq = lastError != null\n    ? AsyncObservable.Throw<int>(lastError)\n    : AsyncObservable.Throw<int>(new InvalidOperationException(\"unknown failure\"));","handlingStrategy":"validation","validationCode":"if (error is null) throw new ArgumentNullException(nameof(error));\n// or pick an alternative sequence:\nvar seq = error != null ? AsyncObservable.Throw<T>(error) : AsyncObservable.Empty<T>();","typeGuard":"static bool IsValidError(Exception e) => e is not null;","tryCatchPattern":"try { var seq = AsyncObservable.Throw<T>(error); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"error\") { /* substitute default exception */ }","preventionTips":["Never persist exceptions in nullable fields without a fallback","Create a sentinel exception when the real one is unavailable","Keep exception-producing helpers contractually non-null-returning"],"tags":["argument-null","exception","reactive-operators"],"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"}