

As a result, a numeric string that is parsed by using the conventions of one culture might fail when using the conventions of another.įor more information about parsing numeric strings, see Parsing Numeric Strings and the documentation for the specific method that threw the exception. Trying to parse a string that contains any other character always throws a FormatException exception.Īll numeric strings are interpreted based on the formatting conventions of a particular culture: either the current thread culture (or, in some cases, the current application domain culture), the invariant culture, or a specified culture. A positive or negative sign, decimal separator, group separators, and currency symbol may also be permitted.

The string to be parsed must consist of the Latin digits 0-9. Numeric types, including all signed integers, unsigned integers, and floating-point types. For more information, see the Guid.Parse method. The string representation of a GUID must consist of 32 hexadecimal digits (0-F), and must be in one of the five formats output by the Guid.ToString method. This means that date and time data saved in a culture-specific format on one system might not parse successfully on another system.įor more information about parsing dates and times, see Parsing Date and Time Strings and the documentation for the method that threw the exception. If it doesn't conform to an expected culture-specific pattern, a FormatException exception is thrown. When you call the DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) and DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) methods, date and time data must also conform exactly to a pattern specified by one or more standard format strings or custom format strings that are provided as arguments in the method call. All date and time data is interpreted based on the formatting conventions of a particular culture: either the current thread culture (or, in some cases, the current application domain culture), the invariant culture, or a specified culture. Any other value throws a FormatException exception.ĭateTime and DateTimeOffset. The Boolean.Parse(String) and Convert.ToBoolean(String) methods require the string to be converted to be "True", "true", "False", or "false". The conversion of a string to the following types in the System namespace can throw a FormatException exception:īoolean. In this case, you should correct the error rather than handle the exception. However, a FormatException exception that is thrown when you're trying to parse a predefined or hard-coded string indicates a program error. You can also replace the call to the conversion method with a call to a TryParse or TryParseExact method, if one exists.
/format-command-prompt-747f5fb312e84087b7473d6ba9bc42db.png)
In most cases, particularly if the string that you're converting is input by a user or read from a file, you should use a try/catch block and handle the FormatException exception if the conversion is unsuccessful. This typically occurs when calling some methods of the Convert class and the Parse and ParseExact methods of some types. In a call to a method that converts a string to some other data type, the string doesn't conform to the required pattern. SerializableAttribute ComVisibleAttribute RemarksĪ FormatException exception can be thrown for one of the following reasons:
