{"record":{"id":"ccac477bce8d381f","repo":"dotnet/wpf","slug":"invalidenumargumentexception-value-int-value-typeof-ccac47","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"value\", (int)value, typeof(ShutdownMode))","messagePattern":"InvalidEnumArgumentException\\(\"value\", \\(int\\)value, typeof\\(ShutdownMode\\)\\)","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":836,"sourceCode":"        ///                             been set - this mode is equivalent to OnExplicitOnly.\n        ///\n        ///         OnExplicitShutdown- this mode will shutdown the application only when an\n        ///                             explicit call to OnShutdown() has been made.\n        /// </summary>\n        public ShutdownMode ShutdownMode\n        {\n            get\n            {\n                VerifyAccess();\n                return _shutdownMode;\n            }\n\n            set\n            {\n                VerifyAccess();\n                if ( !IsValidShutdownMode(value) )\n                {\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof(ShutdownMode));\n                }\n                if (IsShuttingDown || _appIsShutdown)\n                {\n                    throw new InvalidOperationException(SR.ShutdownModeWhenAppShutdown);\n                }\n\n                _shutdownMode = value;\n            }\n        }\n\n        /// <summary>\n        ///     Current locally defined Resources\n        /// </summary>\n        [Ambient]\n        public ResourceDictionary Resources\n        {\n            //Don't use  VerifyAccess() here since Resources can be set from any thread.\n            //We synchronize access using _globalLock","sourceCodeStart":818,"sourceCodeEnd":854,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L818-L854","documentation":"The Application.ShutdownMode property setter verifies the value is a defined ShutdownMode enum member (OnLastWindowClose, OnMainWindowClose, OnExplicitShutdown). Passing an out-of-range integer cast to the enum throws InvalidEnumArgumentException. This prevents the app from entering an undefined shutdown behavior.","triggerScenarios":"Assigning app.ShutdownMode = (ShutdownMode)someInt where someInt is not 0-2, e.g. values loaded from config, parsed from user input, or arithmetic results, without validating the enum range.","commonSituations":"Persisting ShutdownMode to config files and reading back stale/invalid numbers; reflection-based property assignment; enum values changed between library versions.","solutions":["Validate with Enum.IsDefined(typeof(ShutdownMode), value) before assigning","Only assign literal ShutdownMode enum members, not raw ints","Sanitize config-driven integers with a fallback to ShutdownMode.OnLastWindowClose","Call from the UI thread — the setter also requires thread affinity (VerifyAccess)"],"exampleFix":"// before\napp.ShutdownMode = (ShutdownMode)configValue;\n// after\nvar mode = Enum.IsDefined(typeof(ShutdownMode), configValue) ? (ShutdownMode)configValue : ShutdownMode.OnLastWindowClose;\napp.ShutdownMode = mode;","handlingStrategy":"validation","validationCode":"bool isValidMode(int v) => Enum.IsDefined(typeof(ShutdownMode), v);","typeGuard":"bool TryGetShutdownMode(int raw, out ShutdownMode mode) { if (Enum.IsDefined(typeof(ShutdownMode), raw)) { mode = (ShutdownMode)raw; return true; } mode = ShutdownMode.OnLastWindowClose; return false; }","tryCatchPattern":"try { app.ShutdownMode = mode; } catch (InvalidEnumArgumentException ex) { app.ShutdownMode = ShutdownMode.OnLastWindowClose; }","preventionTips":["Validate config integers with Enum.IsDefined","Assign only named enum members","Set on the UI thread","Sanitize persisted enum values on read"],"tags":["wpf","enum","invalid-enum-argument","shutdown","validation"],"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-21T21:30:21.729Z"}