{"record":{"id":"a4d6437b46f4c302","repo":"louthy/language-ext","slug":"key-doesn-t-exist-in-map-key","errorCode":null,"errorMessage":"Key doesn't exist in map: {key}","messagePattern":"Key doesn't exist in map: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs","lineNumber":795,"sourceCode":"    /// </summary>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public TrieMap<EqK, K, V> SetItem(K key, V value) =>\n        Update(key, value, UpdateType.SetItem, false);\n\n    /// <summary>\n    /// Set an item that already exists in the map\n    /// </summary>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public (TrieMap<EqK, K, V> Map, Change<V> Change) SetItemWithLog(K key, V value) =>\n        UpdateWithLog(key, value, UpdateType.SetItem, false);\n        \n    /// <summary>\n    /// Set an item that already exists in the map\n    /// </summary>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public TrieMap<EqK, K, V> SetItem(K key, Func<V, V> Some)\n    {\n        var value = Find(key).Map(Some).IfNone(() => throw new ArgumentException($\"Key doesn't exist in map: {key}\"));\n        return SetItem(key, value);\n    }\n        \n    /// <summary>\n    /// Set an item that already exists in the map\n    /// </summary>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public (TrieMap<EqK, K, V> Map, Change<V> Change) SetItemWithLog(K key, Func<V, V> Some)\n    {\n        var value = Find(key).Map(Some).IfNone(() => throw new ArgumentException($\"Key doesn't exist in map: {key}\"));\n        return SetItemWithLog(key, value);\n    }\n\n    /// <summary>\n    /// Try to set an item that already exists in the map.  If none\n    /// exists, do nothing.\n    /// </summary>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]","sourceCodeStart":777,"sourceCodeEnd":813,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs#L777-L813","documentation":"This error is raised from TrieMap's update path when an operation requiring the key to exist (SetItem semantics) is applied to a key missing from the map, so the update cannot be applied. The faulting input is the key argument that is not present in the TrieMap; use Add or TryFind-style variants when absence is expected.","triggerScenarios":"Calling SetItem with a key absent from the map; Find(key) returns None and the IfNone fallback throws.","commonSituations":"Assuming SetItem behaves like an upsert (as Set does), updating after a key was removed by another code path, or a typo'd/whitespace-differing key.","solutions":["Use Set(key, value) (upsert) instead of SetItem when insertion is acceptable","Check map.Find(key).IsSome before calling SetItem","Use TrySetItem which returns a new map without throwing"],"exampleFix":"// before\nmap = map.SetItem(key, v => v + 1); // throws if key absent\n// after\nmap = map.Set(key, map.Find(key).IfNone(0) + 1); // upsert","handlingStrategy":"try-catch","validationCode":"if (map.Find(key).IsSome) map = map.SetItem(key, f);","typeGuard":null,"tryCatchPattern":"try { map = map.SetItem(key, someFn); }\ncatch (ArgumentException) { map = map.Set(key, defaultValue); } // insert instead","preventionTips":["Use Set() when upsert semantics are intended","Use TrySetItem for non-throwing updates","Check Find(key).IsSome before SetItem"],"tags":["dotnet","triemap","key-not-found","setitem"],"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"}