bchavez/Bogus · error · ArgumentOutOfRangeException

The country code must be an ISO3166 two-letter country code.

Error message

The country code must be an ISO3166 two-letter country code.

What it means

Finance.Iban(countryCode) demands an ISO 3166 two-letter (alpha-2) country code. If countryCode is non-null and its length is not exactly 2 it throws ArgumentOutOfRangeException reporting the actual length. Pass null to let Bogus pick a random IBAN country.

Source

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

   /// </summary>
   /// <param name="formatted">Formatted IBAN containing spaces.</param>
   /// <param name="countryCode">A two letter ISO3166 country code. Throws an exception if the country code is not found or is an invalid length.</param>
   /// <exception cref="KeyNotFoundException">An exception is thrown if the ISO3166 country code is not found.</exception>
   public string Iban(bool formatted = false, string countryCode = null)
   {
      var arr = this.GetArray("iban_formats");

      IBanFormat ibanFormat;
      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)

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Pass a 2-letter ISO 3166 alpha-2 code, e.g. finance.Iban("GB").
  2. Pass null to have a random IBAN country chosen for you.
  3. Normalize/trim the input string and assert its length is 2 before calling.

Example fix

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

// after
var iban = finance.Iban(countryCode: "GB");
Defensive patterns

Strategy: validation

Validate before calling

string countryCode = "GB";
if (countryCode is not null && countryCode.Length != 2)
    throw new ArgumentException("countryCode must be a 2-letter ISO 3166 alpha-2 code or null.");
var iban = finance.Iban(countryCode);

Type guard

static bool IsValidAlpha2(string code) => code is null || (code.Length == 2 && code.All(char.IsLetter));

Prevention

When it happens

Trigger: Calling finance.Iban(countryCode: "GBR") (alpha-3), finance.Iban("UNITED_KINGDOM"), finance.Iban("G"), or finance.Iban("") with a non-null string whose length is not 2.

Common situations: Passing an ISO alpha-3 code (3 letters) or a full country name where an alpha-2 code is required. Copying a code with whitespace or from uppercase mismatched sources.

Related errors


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