litedb-org/LiteDB · error · ArgumentException

Invalid LCID code

Error message

Invalid LCID code

What it means

Thrown by LCID.GetCulture(int lcid) when the supplied LCID integer is not present in the built-in _mappings dictionary. Unlike the others, this throws System.ArgumentException (not LiteException), so a generic catch(LiteException) will miss it. The mapping table is a fixed list of Windows LCID codes compiled into the class.

Source

Thrown at LiteDB/Utils/LCID.cs:457

            [31836] = "chr-Cher",
            [31837] = "iu-Latn",
            [31839] = "tzm-Latn",
            [31847] = "ff-Latn",
            [31848] = "ha-Latn",
            [31878] = "quc-Latn",
            [31890] = "ku-Arab"
            #endregion
        };

        public static CultureInfo GetCulture(int lcid)
        {
            if (_mappings.TryGetValue(lcid, out var name))
            {
                return new CultureInfo(name);
            }
            else
            {
                throw new ArgumentException("Invalid LCID code");
            }
        }

        public static int GetLCID(string culture)
        {
            foreach(var item in _mappings)
            {
                if (item.Value == culture)
                {
                    return item.Key;
                }
            }

            throw new LiteException(0, $"Invalid culture name");
        }

        /// <summary>
        /// Get current system operation LCID culture

View on GitHub (pinned to f906a5f850)

Solutions

  1. Validate the LCID against LCID's known cultures before calling GetCulture, or catch ArgumentException.
  2. Use a culture name (string) path (CultureInfo.GetCultureInfo) if the LCID table is incomplete for your locale.
  3. Rebuild/re-index with a supported collation.
  4. Update/extend the LCID mapping table if a legitimate code is missing.

Example fix

// before
var c = LCID.GetCulture(userLcid); // throws ArgumentException if unknown

// after
try { var c = LCID.GetCulture(userLcid); }
catch (ArgumentException) { c = CultureInfo.InvariantCulture; }
Defensive patterns

Strategy: validation

Validate before calling

// validate before calling; fall back gracefully
CultureInfo c;
try { c = LCID.GetCulture(lcid); }
catch (ArgumentException) { c = CultureInfo.InvariantCulture; }

Try / catch

try { var c = LCID.GetCulture(lcid); }
catch (ArgumentException) {
    // NOTE: not a LiteException - use a separate catch
    c = CultureInfo.InvariantCulture;
}

Prevention

When it happens

Trigger: Calling LCID.GetCulture with an LCID not in the static map; reading a collation LCID from a database/index that references a culture the runtime table doesn't carry; passing a user-supplied locale id without validation.

Common situations: An index created with a culture whose LCID isn't in this LiteDB build's table; cross-platform locale differences; custom/neutral locale IDs; version skew where the LCID table differs.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/16b56e754c91e82c. Report an issue: GitHub.