Share via


Troubleshooting Exceptions: System.FormatException

A FormatException exception is thrown by a method when the format of an argument does not meet the parameter specifications of the method.

For example, many of the data types defined in the System namespace include a Parse method that takes a string argument and converts it to the data type. These methods throw a FormatException if the supplied argument is not in a format that can be converted. Double.Parse will throw a FormatException if its string argument is not in a recognizable numeric format. Consider the following examples.

' The first three statements run correctly.

Console.WriteLine(Double.Parse("32,115"))

Console.WriteLine(Double.Parse("32115"))

Console.WriteLine(Double.Parse("32.115"))

' The following statement throws a FormatException.

' Console.WriteLine(Double.Parse("32 115"))

Similarly, Boolean.Parse throws this exception if the string argument is not either "True" or "False".

' This statement runs correctly.

Console.WriteLine(Boolean.Parse("True"))

' This statement throws a FormatException.

' Console.WriteLine(Boolean.Parse("Ture"))

Associated Tips

  • Make sure your method arguments are in the correct format.
    The format of your method arguments must meet the parameter specifications of the invoked members.

See Also

Tasks

How to: Use the Exception Assistant

Reference

FormatException

Double.Parse

Boolean.Parse