什么是FormatException
参数格式无效或复合格式字符串不正确时引发的异常。
- 继承
-
ObjectExceptionSystemExceptionFormatException
详细说明
FormatException
-
Convert类的某些方法和某些类型的
ParseExactParse和方法时。在这种情况下, 应更正此错误, 而不是处理异常。
FormatException引发异常:
-
-
FormatException引发异常。
-
这意味着, 在一个系统上保存为特定于区域性的格式的日期和时间数据可能无法在其他系统上成功分析。
有关分析日期和时间的详细信息, 请参阅分析日期和时间字符串和引发异常的方法的文档。
-
Guid.Parse 方法。
-
FormatException异常。
因此, 使用另一种区域性的约定分析的数值字符串可能会失败。
有关分析数值字符串的详细信息, 请参阅分析数值字符串和引发异常的特定方法的文档。
-
TimeSpan分析方法的文档。
-
-
-
using System; public class Example { public static void Main() { decimal price = 169.32m; Console.WriteLine("The cost is {0:Q2}.", price); } } // The example displays the following output: // Unhandled Exception: System.FormatException: Format specifier was invalid. // at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info) // at System.Decimal.ToString(String format, IFormatProvider provider) // at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) // at System.IO.TextWriter.WriteLine(String format, Object arg0) // at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0) // at Example.Main()
using System; public class Example { public static void Main() { decimal price = 169.32m; Console.WriteLine("The cost is {0:C2}.", price); } } // The example displays the following output: // The cost is $169.32.
using System; public class Example { public static void Main() { string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"; Console.WriteLine(Guid.ParseExact(guidString, "G")); } } // The example displays the following output: // Unhandled Exception: System.FormatException: // Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x". // at System.Guid.ParseExact(String input, String format) // at Example.Main()
using System; public class Example { public static void Main() { string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"; Console.WriteLine(Guid.Parse(guidString)); } } // The example displays the following output: // ba748d5c-ae5f-4cca-84e5-1ac5291c38cb
-
FormatException导致异常:。
using System; public class Example { public enum TemperatureScale { Celsius, Fahrenheit, Kelvin } public static void Main() { String info = GetCurrentTemperature(); Console.WriteLine(info); } private static String GetCurrentTemperature() { DateTime dat = DateTime.Now; Decimal temp = 20.6m; TemperatureScale scale = TemperatureScale.Celsius; String result; result = String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}", dat, temp, scale); return result; } } // The example displays output like the following: // Unhandled Exception: System.FormatException: Format specifier was invalid. // at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info) // at System.Decimal.ToString(String format, IFormatProvider provider) // at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) // at System.String.Format(IFormatProvider provider, String format, Object[] args) // at Example.Main()
using System; public class Example { public enum TemperatureScale { Celsius, Fahrenheit, Kelvin } public static void Main() { String info = GetCurrentTemperature(); Console.WriteLine(info); } private static String GetCurrentTemperature() { DateTime dat = DateTime.Now; Decimal temp = 20.6m; TemperatureScale scale = TemperatureScale.Celsius; String result; result = String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}", dat, temp, scale); return result; } } // The example displays output like the following: // At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius
-
应更正此情况, 而不是在
try/catch块中进行处理。如以下示例中所示, 尝试在字符串中包含文本大括号会引发异常。
例如, 您可以修改上一复合格式字符串, 如下所示。result = String.Format("The text has {0} '{' characters and {1} '}' characters.", nOpen, nClose);
string result; int nOpen = 1; int nClose = 2; result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.", nOpen, nClose); Console.WriteLine(result);
int n1 = 10; int n2 = 20; String result = String.Format("{0 + {1] = {2}", n1, n2, n1 + n2);
若要更正此错误, 请确保所有左大括号和右大括号对应。
String result = String.Format("{0} + {1} = {2}", n1, n2, n1 + n2);
-
Int32的参数数组具有四个元素。
using System; using System.Collections.Generic; public class Example { public static void Main() { Random rnd = new Random(); int[] numbers = new int[4]; int total = 0; for (int ctr = 0; ctr <= 2; ctr++) { int number = rnd.Next(1001); numbers[ctr] = number; total += number; } numbers[3] = total; Console.WriteLine("{0} + {1} + {2} = {3}", numbers); } } // The example displays the following output: // Unhandled Exception: // System.FormatException: // Index (zero based) must be greater than or equal to zero and less than the size of the argument list. // at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) // at System.IO.TextWriter.WriteLine(String format, Object arg0) // at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0) // at Example.Main()
using System; using System.Collections.Generic; public class Example { public static void Main() { Random rnd = new Random(); int[] numbers = new int[4]; int total = 0; for (int ctr = 0; ctr <= 2; ctr++) { int number = rnd.Next(1001); numbers[ctr] = number; total += number; } numbers[3] = total; object[] values = new object[numbers.Length]; numbers.CopyTo(values, 0); Console.WriteLine("{0} + {1} + {2} = {3}", values); } } // The example displays output like the following: // 477 + 956 + 901 = 2334
HRESULT
FormatException使用 COR_E_FORMAT 值为0x80131537 的 HRESULT。