{"record":{"id":"655eb0ed4305cb10","repo":"louthy/language-ext","slug":"key-not-found-in-set","errorCode":null,"errorMessage":"Key not found in set","messagePattern":"Key not found in set","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs","lineNumber":1092,"sourceCode":"        {\n            return Contains<OrdK, K>(node.Left, key);\n        }\n        else if (cmp > 0)\n        {\n            return Contains<OrdK, K>(node.Right, key);\n        }\n        else\n        {\n            return true;\n        }\n    }\n\n    [Pure]\n    public static K Find<OrdK, K>(SetItem<K> node, K key) where OrdK : Ord<K>\n    {\n        if (node.IsEmpty)\n        {\n            throw new ArgumentException(\"Key not found in set\");\n        }\n        var cmp = OrdK.Compare(key, node.Key);\n        if (cmp < 0)\n        {\n            return Find<OrdK, K>(node.Left, key);\n        }\n        else if (cmp > 0)\n        {\n            return Find<OrdK, K>(node.Right, key);\n        }\n        else\n        {\n            return node.Key;\n        }\n    }\n\n    /// <summary>\n    /// TODO: I suspect this is suboptimal, it would be better with a custom Enumerator ","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs#L1074-L1110","documentation":"This ArgumentException is a lookup guard in Set.Internal's Find<OrdK, K>: when the recursive search reaches an empty node the key is absent, and since Find must return a K (not an Option), it throws instead of returning a default. The faulting input is a key that is not in the set; callers should use Contains or TryFind first.","triggerScenarios":"Set.Find (find) called with a key that is not in the set, including lookups on an empty set; also triggered by comparer mismatch making the key 'unfindable'.","commonSituations":"Searching for a value never inserted, lookup key differing in case/culture from the stored one, or an Ord type argument different from the one used when inserting.","solutions":["Use set.Find(key) returning Option<K> / TryFind instead of the throwing Find","Check set.Contains(key) before Find","Use the same Ord instance for lookup as was used for insertion"],"exampleFix":"// before\nvar v = set.Find(key); // throws if absent\n// after\nset.Find(key).IfSome(v => ...); // Option-based lookup","handlingStrategy":"fallback","validationCode":"if (set.Contains(key)) { var v = Set.find(key, set); }","typeGuard":null,"tryCatchPattern":"K v;\ntry { v = Set.find(key, set); }\ncatch (ArgumentException) { v = default; } // or Option-based lookup","preventionTips":["Prefer Option-returning Find/TryFind over throwing Find","Use the same Ord instance for insert and lookup","Guard against case/culture differences in keys"],"tags":["dotnet","set","key-not-found","lookup"],"backgroundTag":"key-not-found","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}