{"record":{"id":"df3eff534c669333","repo":"bchavez/Bogus","slug":"the-iso3166-iban-country-code-countrycode-was","errorCode":null,"errorMessage":"The ISO3166 IBAN country code '{countryCode}' was not found.","messagePattern":"The ISO3166 IBAN country code '(.+?)' was not found\\.","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/DataSets/Finance.cs","lineNumber":398,"sourceCode":"      if( countryCode is null )\n      {\n         var formatEntry = (BObject)this.Random.ArrayElement(arr);\n         ibanFormat = this.GetIbanFormat(formatEntry);\n      }\n      else\n      {\n         if( countryCode.Length != 2 )\n         {\n            throw new ArgumentOutOfRangeException(nameof(countryCode), countryCode.Length, \"The country code must be an ISO3166 two-letter country code.\");\n         }\n\n         var formatEntry = arr.OfType<BObject>()\n            .Where(b => countryCode.Equals(b[\"country\"].StringValue, StringComparison.OrdinalIgnoreCase))\n            .FirstOrDefault();\n\n         if (formatEntry is null)\n         {\n            throw new KeyNotFoundException($\"The ISO3166 IBAN country code '{countryCode}' was not found.\");\n         }\n\n         ibanFormat = this.GetIbanFormat(formatEntry);\n      }\n\n      return Iban(ibanFormat, formatted);\n   }\n\n   protected string Iban(IBanFormat ibanFormat, bool formatted)\n   {\n      var stringBuilder = new StringBuilder();\n      var count = 0;\n      for (var b = 0; b < ibanFormat.Bban.Length; b++)\n      {\n         var bban = ibanFormat.Bban[b];\n         var c = bban.Count;\n         count += bban.Count;\n         while (c > 0)","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/DataSets/Finance.cs#L380-L416","documentation":"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.","triggerScenarios":"Calling finance.Iban(\"US\"), finance.Iban(\"AU\"), or any 2-letter code for a country that does not participate in IBAN.","commonSituations":"Assuming every ISO alpha-2 code has an IBAN format. Feeding user-supplied country codes without filtering to IBAN-capable countries.","solutions":["Use a country known to have an IBAN format, e.g. finance.Iban(\"GB\"), finance.Iban(\"DE\"), finance.Iban(\"FR\").","Pass null to let Bogus select a random IBAN-supporting country.","Pre-filter input against the iban_formats list (finance.GetArray(\"iban_formats\")) before calling."],"exampleFix":"// before\nvar iban = finance.Iban(countryCode: \"US\");\n\n// after\nvar iban = finance.Iban(countryCode: null); // random IBAN-capable country","handlingStrategy":"validation","validationCode":"string countryCode = \"GB\";\nvar arr = finance.GetArray(\"iban_formats\");\nbool hasIban = arr.OfType<BObject>().Any(b => b[\"country\"].StringValue.Equals(countryCode, StringComparison.OrdinalIgnoreCase));\nif (!hasIban) throw new KeyNotFoundException($\"No IBAN format for {countryCode}\");\nvar iban = finance.Iban(countryCode);","typeGuard":null,"tryCatchPattern":"try { return finance.Iban(countryCode); }\ncatch (KeyNotFoundException) { return finance.Iban(countryCode: null); }","preventionTips":["Assume not every alpha-2 code has an IBAN format (US/AU/CA do not).","Pass null to let Bogus pick a valid IBAN country.","Maintain a whitelist of IBAN-capable countries if accepting user input."],"tags":["finance","iban","data-lookup","iso3166"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}