{"record":{"id":"f574c759cf7b49a2","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-exception","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'exception')","messagePattern":"Value cannot be null\\. \\(Parameter 'exception'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Throw.cs","lineNumber":20,"sourceCode":"// 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.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Returns a sequence that throws an exception upon enumeration.\n        /// </summary>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"exception\">Exception to throw upon enumerating the resulting sequence.</param>\n        /// <returns>Sequence that throws the specified exception upon enumeration.</returns>\n        public static IEnumerable<TResult> Throw<TResult>(Exception exception)\n        {\n            if (exception == null)\n                throw new ArgumentNullException(nameof(exception));\n\n            return ThrowCore<TResult>(exception);\n        }\n\n        private static IEnumerable<TResult> ThrowCore<TResult>(Exception exception)\n        {\n            throw exception;\n#pragma warning disable CS0162 // Unreachable code detected\n            yield break;\n#pragma warning restore CS0162 // Unreachable code detected\n        }\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Throw.cs#L2-L34","documentation":"Throw<TResult>(exception) creates a sequence that throws the given exception when enumerated. Because the whole point is to throw a caller-supplied exception, a null exception is meaningless; the factory validates it eagerly and throws ArgumentNullException naming `exception`.","triggerScenarios":"Calling `Throw<MyType>(null)`, e.g. rethrowing a captured exception variable that turned out to be null, or forwarding `ex` from a handler that received null.","commonSituations":"Generic error-propagation pipelines where the captured Exception field/property defaults to null, building a stream of failures from nullable exception state (e.g. Task.Exception before fault).","solutions":["Ensure a concrete Exception instance is passed, e.g. `Throw<int>(new InvalidOperationException(\"...\")`.","If the exception variable may be null, fall back to a default exception: `ex ?? new Exception(\"unknown\")`.","Rework the pipeline to only enter the Throw path when an exception was actually captured.","For deferred failure, consider Task/async compositions that carry failure natively instead of a null placeholder."],"exampleFix":"// before\nreturn Throw<int>(lastError);\n// after\nreturn Throw<int>(lastError ?? new InvalidOperationException(\"Operation failed with no details.\"));","handlingStrategy":"validation","validationCode":"if (exception is null) throw new ArgumentNullException(nameof(exception));\n// or: var ex2 = exception ?? new InvalidOperationException(\"no details\");","typeGuard":"static bool IsValidException(Exception e) => e is not null;","tryCatchPattern":"try { var seq = Throw<T>(captured); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"exception\") { /* supply a fallback exception */ }","preventionTips":["Never store Exception fields that can be observed as null; initialize with a default.","Check Task.Exception only after IsFaulted.","Default null exceptions with `?? new Exception(...)` in propagation code.","Log why the exception was null to find the upstream capture bug."],"tags":["linq","null-argument","ix-interactive"],"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"}