dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("value", (int)value…
Error message
InvalidEnumArgumentException("value", (int)value, typeof(ShutdownMode)) What it means
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.
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)
Example fix
// before app.ShutdownMode = (ShutdownMode)configValue; // after var mode = Enum.IsDefined(typeof(ShutdownMode), configValue) ? (ShutdownMode)configValue : ShutdownMode.OnLastWindowClose; app.ShutdownMode = mode;
Defensive patterns
Strategy: validation
Validate before calling
bool isValidMode(int v) => Enum.IsDefined(typeof(ShutdownMode), v);
Type guard
bool TryGetShutdownMode(int raw, out ShutdownMode mode) { if (Enum.IsDefined(typeof(ShutdownMode), raw)) { mode = (ShutdownMode)raw; return true; } mode = ShutdownMode.OnLastWindowClose; return false; } Try / catch
try { app.ShutdownMode = mode; } catch (InvalidEnumArgumentException ex) { app.ShutdownMode = ShutdownMode.OnLastWindowClose; } Prevention
- Validate config integers with Enum.IsDefined
- Assign only named enum members
- Set on the UI thread
- Sanitize persisted enum values on read
When it happens
Trigger: 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.
Common situations: Persisting ShutdownMode to config files and reading back stale/invalid numbers; reflection-based property assignment; enum values changed between library versions.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- ThemeMode value is invalid. Use None, System, Light or Dark
- InvalidEnumArgumentException(argumentName, direction…
- InvalidEnumArgumentException("mouseAction"…
- InvalidEnumArgumentException(param, edge, ElementEdge)
- SR.Storyboard_UnrecognizedHandoffBehavior
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ccac477bce8d381f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:836
/// been set - this mode is equivalent to OnExplicitOnly.
///
/// OnExplicitShutdown- this mode will shutdown the application only when an
/// explicit call to OnShutdown() has been made.
/// </summary>
public ShutdownMode ShutdownMode
{
get
{
VerifyAccess();
return _shutdownMode;
}
set
{
VerifyAccess();
if ( !IsValidShutdownMode(value) )
{
throw new InvalidEnumArgumentException("value", (int)value, typeof(ShutdownMode));
}
if (IsShuttingDown || _appIsShutdown)
{
throw new InvalidOperationException(SR.ShutdownModeWhenAppShutdown);
}
_shutdownMode = value;
}
}
/// <summary>
/// Current locally defined Resources
/// </summary>
[Ambient]
public ResourceDictionary Resources
{
//Don't use VerifyAccess() here since Resources can be set from any thread.
//We synchronize access using _globalLockView on GitHub (pinned to 81131a70a4)