{"record":{"id":"fdbfb8a5146b2201","repo":"dotnet/wpf","slug":"invalidenumargumentexception-modifiers-int-modifiers-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"modifiers\", (int)modifiers, typeof(ModifierKeys))","messagePattern":"InvalidEnumArgumentException\\(\"modifiers\", \\(int\\)modifiers, typeof\\(ModifierKeys\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs","lineNumber":91,"sourceCode":"        /// <param name=\"key\">Key</param>\n        /// <param name=\"modifiers\">Modifiers</param>\n        /// <param name=\"validateGesture\">If true, throws an exception if the key and modifier are not valid</param>\n        internal KeyGesture(Key key, ModifierKeys modifiers, bool validateGesture)\n            : this(key, modifiers, String.Empty, validateGesture)\n        {\n        }\n\n        /// <summary>\n        /// Private constructor that does the real work.\n        /// </summary>\n        /// <param name=\"key\">Key</param>\n        /// <param name=\"modifiers\">Modifiers</param>\n        /// <param name=\"displayString\">display string</param>\n        /// <param name=\"validateGesture\">If true, throws an exception if the key and modifier are not valid</param>\n        private KeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture)\n        {\n            if(!ModifierKeysConverter.IsDefinedModifierKeys(modifiers))\n                throw new InvalidEnumArgumentException(\"modifiers\", (int)modifiers, typeof(ModifierKeys));\n\n            if(!IsDefinedKey(key))\n                throw new InvalidEnumArgumentException(\"key\", (int)key, typeof(Key));\n\n            ArgumentNullException.ThrowIfNull(displayString);\n\n            if (validateGesture && !IsValid(key, modifiers))\n            {\n                throw new NotSupportedException(SR.Format(SR.KeyGesture_Invalid, modifiers, key));\n            }\n\n            _modifiers = modifiers;\n            _key = key;\n            _displayString = displayString;\n        }\n#endregion Constructors\n\n        //------------------------------------------------------","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs#L73-L109","documentation":"The private KeyGesture constructor validates that the ModifierKeys value is a defined combination (via ModifierKeysConverter.IsDefinedModifierKeys) and throws InvalidEnumArgumentException for 'modifiers' otherwise. This catches out-of-range or undefined bit values cast into ModifierKeys.","triggerScenarios":"Calling a public KeyGesture constructor with (ModifierKeys)value where the int is not a defined enum combination (e.g. (ModifierKeys)99 or negative values).","commonSituations":"Deserializing gestures from config where modifier ints come from user data or another system's flag values.","solutions":["Only pass ModifierKeys values composed of the defined enum members (None, Alt, Control, Shift, Windows)","Validate with ModifierKeysConverter.IsDefinedModifierKeys before constructing","Clamp/parse modifiers from config through the enum converter rather than raw casts"],"exampleFix":"// before\nvar gesture = new KeyGesture(Key.A, (ModifierKeys)rawInt);\n// after\nvar modifiers = (ModifierKeys)rawInt;\nif (ModifierKeysConverter.IsDefinedModifierKeys(modifiers))\n{\n    var gesture = new KeyGesture(Key.A, modifiers);\n}","handlingStrategy":"validation","validationCode":"if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) throw new ArgumentOutOfRangeException(nameof(modifiers));","typeGuard":"static bool IsDefinedModifiers(ModifierKeys m) => ModifierKeysConverter.IsDefinedModifierKeys(m);","tryCatchPattern":"try { var g = new KeyGesture(key, modifiers); } catch (InvalidEnumArgumentException ex) { /* undefined modifiers value */ }","preventionTips":["Never cast raw ints to ModifierKeys without validation","Compose modifiers only from defined enum members","Parse modifier strings through ModifierKeysConverter"],"tags":["wpf","input","enum","keygesture"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}