FormatException
C# Programming Language
Severity: MinorWhat Does This Error Mean?
FormatException means you tried to convert a value — usually a string — into another type, but the format was wrong. For example: trying to convert the text 'hello' into a number, or a date string that uses the wrong separator. C# cannot guess what you meant — the format must match exactly. The fix is usually to use a safer conversion method that does not crash on bad input.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- Calling int.Parse() or double.Parse() on a string that contains letters or symbols
- Passing a date string in the wrong format to DateTime.Parse() — for example '28-03-2026' when 'MM/dd/yyyy' is expected
- Converting user input that contains unexpected characters like spaces, commas, or currency symbols
- Reading data from a CSV or external file where a value is missing or malformed
- Using String.Format() with a format string that does not match the number of arguments provided
How to Fix It
-
Replace int.Parse(), double.Parse(), or DateTime.Parse() with their TryParse() equivalents. TryParse() returns true or false instead of crashing.
Example: if (int.TryParse(myString, out int result)) { use result } else { handle the bad value }
-
If you must use Parse(), wrap it in a try-catch block so the program handles the error gracefully instead of crashing.
try { int value = int.Parse(input); } catch (FormatException) { show an error message to the user }
-
For date strings, specify the exact format you expect using DateTime.ParseExact() or DateTime.TryParseExact(). This removes all ambiguity.
Example: DateTime.TryParseExact(input, 'yyyy-MM-dd', CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date)
-
Clean up the string before parsing if it contains extra characters. Remove spaces, commas, currency symbols, or percent signs that would confuse the parser.
Example: string cleaned = input.Trim().Replace(',', '').Replace('$', ''); — then parse the cleaned string.
-
Validate data at the source. If data comes from a form, file, or API, check it before passing it to conversion methods. Reject or flag bad data early.
A simple check like 'is this string all digits?' before calling int.Parse() prevents most FormatExceptions.
When to Call a Professional
FormatException is a code bug you can fix yourself. The key is using TryParse() instead of Parse() for user-supplied or external data. If you are working with date formats from different countries or locales, a developer with internationalization experience can help set up proper culture-aware parsing.
Frequently Asked Questions
What is the difference between Parse() and TryParse()?
Parse() converts a string and throws an exception if it fails. TryParse() attempts the conversion and returns true if it worked, false if it did not — no exception. For data you control (like a constant), Parse() is fine. For any data from users, files, or external sources, always use TryParse() to avoid crashes.
Why does DateTime.Parse() fail on some computers but not others?
DateTime.Parse() uses the computer's regional settings (culture) to understand date formats. On a US computer, '03/28/2026' means March 28. On a UK computer, the same string means March 28 too — but '28/03/2026' is UK style and would fail on a US machine. Always use CultureInfo.InvariantCulture with a specific format string to get consistent results everywhere.
How do I handle FormatException from a CSV file with bad data?
Wrap each row's parsing in a try-catch block. Log the row number and the bad value when parsing fails. Decide whether to skip bad rows, use a default value, or stop processing. For production code, collect all errors and report them at the end rather than crashing on the first bad row.