stride3d/stride · error · KeyNotFoundException

Settings key not found

Error message

Settings key not found

What it means

SettingsKeyT.GetValue walks the profile chain (profile values, then parent profiles, then the default value) and this KeyNotFoundException marks a terminal state that the library considers unreachable: the key reached the root profile without finding a slot or returning DefaultValue. It indicates corrupted key/container registration state.

Solutions

  1. Register the key properly with the container before reading (create keys via the container's supported registration path)
  2. Re-save/reload the settings file with the current app version so key metadata matches
  3. Update code that hit the catch-and-fallthrough path to return DefaultValue instead of continuing to the unreachable throw
  4. Report/investigate as a library bug if it reproduces with normal registered keys

Example fix

// before
var v = myKey.GetValue(profile); // KeyNotFoundException on stale key
// after
var v;
try { v = myKey.GetValue(profile); }
catch (KeyNotFoundException) { v = myKey.DefaultValue; }
Defensive patterns

Strategy: try-catch

Validate before calling

var value = myKey.TryGetValue ? myKey.DefaultValue : myKey.GetValue(profile); // if a safe accessor exists, prefer it

Try / catch

try { v = myKey.GetValue(profile); }
catch (KeyNotFoundException) { v = myKey.DefaultValue; }

Prevention

When it happens

Trigger: Calling GetValue on a key whose lookup path is broken — e.g. a key whose Container/registration state is inconsistent, or a profile chain where no level can answer for the key.

Common situations: Deserializing a settings profile saved by a different app version whose key set changed; manually constructing or patching SettingsKey/SettingsProfile internals in tests; a bug where the key was never registered with the container.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/631e92aaa7a92a03. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Design/Settings/SettingsKey.cs:230

    /// Gets the value of this settings key in the current profile.
    /// </summary>
    /// <returns>The value of this settings key.</returns>
    public T GetValue()
    {
        var profile = ResolveProfile();
        if (profile.GetValue(Name, out var value, true, false))
        {
            try
            {
                return (T)value;
            }
            catch (Exception)
            {
                return DefaultValue;
            }
        }
        // This should never happen
        throw new KeyNotFoundException("Settings key not found");
    }

    /// <summary>
    /// Sets the value of this settings key in the given profile.
    /// </summary>
    /// <param name="value">The new value to set.</param>
    /// <param name="profile">The profile in which to set the value. Must be a non-null that uses the same <see cref="SettingsContainer"/> that this <see cref="SettingsKey"/>.</param>
    public void SetValue(T value, SettingsProfile profile)
    {
#if NET6_0_OR_GREATER
        ArgumentNullException.ThrowIfNull(profile);
#else
        if (profile is null) throw new ArgumentNullException(nameof(profile));
#endif
        profile = ResolveProfile(profile);
        profile.SetValue(Name, value);
    }

View on GitHub (pinned to 96fad776d2)