bchavez/Bogus · error · KeyNotFoundException

The ISO3166 IBAN country code '{countryCode}' was not found.

Error message

The ISO3166 IBAN country code '{countryCode}' was not found.

What it means

The supplied countryCode has the correct length (2) but does not match any "country" entry in the bundled iban_formats data, so no IBAN structure exists for it. Bogus throws KeyNotFoundException. Many countries (e.g. US, AU, CA) simply have no IBAN format.

Source

Thrown at Source/Bogus/DataSets/Finance.cs:398

      if( countryCode is null )
      {
         var formatEntry = (BObject)this.Random.ArrayElement(arr);
         ibanFormat = this.GetIbanFormat(formatEntry);
      }
      else
      {
         if( countryCode.Length != 2 )
         {
            throw new ArgumentOutOfRangeException(nameof(countryCode), countryCode.Length, "The country code must be an ISO3166 two-letter country code.");
         }

         var formatEntry = arr.OfType<BObject>()
            .Where(b => countryCode.Equals(b["country"].StringValue, StringComparison.OrdinalIgnoreCase))
            .FirstOrDefault();

         if (formatEntry is null)
         {
            throw new KeyNotFoundException($"The ISO3166 IBAN country code '{countryCode}' was not found.");
         }

         ibanFormat = this.GetIbanFormat(formatEntry);
      }

      return Iban(ibanFormat, formatted);
   }

   protected string Iban(IBanFormat ibanFormat, bool formatted)
   {
      var stringBuilder = new StringBuilder();
      var count = 0;
      for (var b = 0; b < ibanFormat.Bban.Length; b++)
      {
         var bban = ibanFormat.Bban[b];
         var c = bban.Count;
         count += bban.Count;
         while (c > 0)

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Use a country known to have an IBAN format, e.g. finance.Iban("GB"), finance.Iban("DE"), finance.Iban("FR").
  2. Pass null to let Bogus select a random IBAN-supporting country.
  3. Pre-filter input against the iban_formats list (finance.GetArray("iban_formats")) before calling.

Example fix

// before
var iban = finance.Iban(countryCode: "US");

// after
var iban = finance.Iban(countryCode: null); // random IBAN-capable country
Defensive patterns

Strategy: validation

Validate before calling

string countryCode = "GB";
var arr = finance.GetArray("iban_formats");
bool hasIban = arr.OfType<BObject>().Any(b => b["country"].StringValue.Equals(countryCode, StringComparison.OrdinalIgnoreCase));
if (!hasIban) throw new KeyNotFoundException($"No IBAN format for {countryCode}");
var iban = finance.Iban(countryCode);

Try / catch

try { return finance.Iban(countryCode); }
catch (KeyNotFoundException) { return finance.Iban(countryCode: null); }

Prevention

When it happens

Trigger: Calling finance.Iban("US"), finance.Iban("AU"), or any 2-letter code for a country that does not participate in IBAN.

Common situations: Assuming every ISO alpha-2 code has an IBAN format. Feeding user-supplied country codes without filtering to IBAN-capable countries.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/df3eff534c669333. Report an issue: GitHub.