什么是FormatException

参数格式无效或复合格式字符串不正确时引发的异常。

继承
Object
Exception
SystemException

FormatException

详细说明

由于以下原因之一, 可能会引发异常:FormatException

  • 在对将字符串转换为其他数据类型的方法的调用中, 该字符串不符合所需模式。 这通常发生在调用Convert类的某些方法和某些类型的ParseExact Parse和方法时。

    在大多数情况下, 特别是当要转换的字符串是用户输入或从文件中读取时, 应使用try/catch块并FormatException处理异常 (如果转换不成功)。 你还可以将对转换方法的调用替换为对TryParseTryParseExact方法的调用 (如果存在)。 但是, FormatException当你尝试分析预定义或硬编码字符串时引发的异常指示程序错误。 在这种情况下, 应更正此错误, 而不是处理异常。

    将字符串转换为System命名空间中的以下类型可能会FormatException引发异常:

      • Boolean。 Boolean.Parse(String) 和Convert.ToBoolean(String)方法要求将字符串转换为 "true"、"true"、"false" 或 "false"。 任何其他值都将FormatException引发异常。

      • DateTime 和 DateTimeOffset。 所有日期和时间数据都基于特定区域性的格式设置约定: 当前线程区域性 (或在某些情况下为当前应用程序域区域性)、固定区域性或指定的区域性。 在调用DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)和方法时, 日期和时间数据还必须完全符合由一个或多个标准格式字符串或自定义格式字符串(作为中的参数提供) 指定的模式方法调用。 如果它不符合预期的区域性特定模式, FormatException则会引发异常。 这意味着, 在一个系统上保存为特定于区域性的格式的日期和时间数据可能无法在其他系统上成功分析。

        有关分析日期和时间的详细信息, 请参阅分析日期和时间字符串和引发异常的方法的文档。

      • Guid. GUID 的字符串表示形式必须包含32个十六进制数字 (0-F), 并且必须是由Guid.ToString方法输出的五种格式中的一种。 有关更多信息,请参见 Guid.Parse 方法。

      • 数值类型, 包括所有有符号整数、无符号整数和浮点类型。 要分析的字符串必须包含拉丁语位数0-9。 还可以允许使用正号或负号、小数点分隔符、组分隔符和货币符号。 尝试分析包含任何其他字符的字符串始终会引发FormatException异常。

        所有数字字符串都基于特定区域性的格式设置约定: 当前线程区域性 (或在某些情况下为当前应用程序域区域性)、固定区域性或指定的区域性。 因此, 使用另一种区域性的约定分析的数值字符串可能会失败。

        有关分析数值字符串的详细信息, 请参阅分析数值字符串和引发异常的特定方法的文档。

      • 时间间隔。 要分析的字符串必须是采用不区分区域性的格式或由当前线程区域性定义的区分区域性的格式 (在某些情况下为当前应用程序域区域性)、固定区域性或指定的区域性。 如果字符串的格式不正确, 或者如果时间间隔的最小值、天数、小时数和分钟数部分不存在, 分析方法将引发FormatException异常。 有关详细信息, 请参阅引发异常的TimeSpan分析方法的文档。

  • 类型实现IFormattable接口, 该接口支持定义如何将对象转换为其字符串表示形式, 并使用无效格式字符串的格式字符串。 这在格式设置操作中最常见。 在下面的示例中, 在复合格式字符串中使用 "Q" 标准格式字符串来设置数值的格式。 但是, "Q" 不是有效的标准格式字符串。

    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()

    此异常是由编码错误引起的。 若要更正此错误, 请删除格式字符串或替换有效的字符串。 下面的示例通过使用 "C" (货币) 格式字符串替换无效的格式字符串来纠正错误。

    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.

    还可以通过分析方法 ( DateTime.ParseExact如和Guid.ParseExact) 引发异常,这需要对字符串进行分析,使其符合格式字符串指定的模式。FormatException 在下面的示例中, GUID 的字符串表示形式应符合 "G" 标准格式字符串指定的模式。 但是, Guid结构的IFormattable实现不支持 "G" 格式字符串。

    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()

    此异常也是由于编码错误引起的。 若要更正此错误, 请调用不需要精确格式的分析方法 (如DateTime.Parse或Guid.Parse) 或替换有效的格式字符串。 下面的示例通过调用Guid.Parse方法来更正错误。

    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
  • 复合格式字符串中的格式项的一个或多个索引大于对象列表或参数数组中项的索引。 在下面的示例中, 格式字符串中的格式项的最大索引为3。 由于对象列表中项的索引是从零开始的, 因此此格式字符串要求对象列表具有四项。 相反, 它只有三个、 dat temp、和scale, 因此, 代码会在运行时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()

    在这种情况下FormatException , 异常是由开发人员错误引起的。 应更正此情况, 而不是通过try/catch确保对象列表中的每个项对应于格式项的索引, 而不是在块中进行处理。 若要更正此示例, 请将第二个格式项的索引改为dat引用变量, 并将每个后续格式项的索引减一。

    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
  • 复合格式字符串的格式不正确。 发生这种情况时FormatException , 异常始终是由开发人员错误引起的。 应更正此情况, 而不是在try/catch块中进行处理。

    如以下示例中所示, 尝试在字符串中包含文本大括号会引发异常。

    result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
    nOpen, nClose);

    在复合格式字符串中包含大括号的建议方法是将它们包含在对象列表中, 并使用格式项将它们插入到结果字符串中。 例如, 您可以修改上一复合格式字符串, 如下所示。

    string result;
    int nOpen = ;
    int nClose = ;
    result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
    nOpen, nClose);
    Console.WriteLine(result);

    如果格式字符串包含打字错误, 也会引发异常。 以下对String.Format方法的调用省略了右大括号, 并对左大括号和右括号配对。

    int n1 = ;
    int n2 = ;
    String result = String.Format("{0 + {1] = {2}",
    n1, n2, n1 + n2);

    若要更正此错误, 请确保所有左大括号和右大括号对应。

    String result = String.Format("{0} + {1} = {2}",
    n1, n2, n1 + n2);
  • 已提供复合格式设置方法中的对象列表作为强类型参数数组, 但该FormatException异常表示一个或多个格式项的索引超过对象列表中的参数数量。 出现这种情况的原因是, 数组类型之间没有显式转换, 因此编译器会将该数组视为单个参数, 而不是作为参数数组。 例如, 以下对Console.WriteLine(String, Object[])方法的调用将FormatException引发异常, 尽管格式项的最高索引为 3, 而类型Int32的参数数组具有四个元素。

    using System;
    using System.Collections.Generic; public class Example
    {
    public static void Main()
    {
    Random rnd = new Random();
    int[] numbers = new int[];
    int total = ;
    for (int ctr = ; ctr <= ; ctr++) {
    int number = rnd.Next();
    numbers[ctr] = number;
    total += number;
    }
    numbers[] = 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()

    应该消除其原因, 而不是处理此异常。 因为既不 Visual Basic C#也不能将整数数组转换为对象数组, 所以必须在调用复合格式设置方法之前自行执行转换。 下面的示例提供了一个实现。

    using System;
    using System.Collections.Generic; public class Example
    {
    public static void Main()
    {
    Random rnd = new Random();
    int[] numbers = new int[];
    int total = ;
    for (int ctr = ; ctr <= ; ctr++) {
    int number = rnd.Next();
    numbers[ctr] = number;
    total += number;
    }
    numbers[] = total;
    object[] values = new object[numbers.Length];
    numbers.CopyTo(values, );
    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。

关于System.FormatException异常的更多相关文章

  1. C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常

    在DataGridView手动添加了CheckBox列;在窗体Show的时候,遇到一个错误:错误如下: DataGridView中发生一下异常:System.FormatException:单元格的F ...

  2. “System.FormatException”类型的未经处理的异常在 System.IdentityModel.dll 中发生 其他信息: 十六进制字符串格式无效。

    如果你的 WebService 客户端证书配置都没问题,唯独调用接口会出现这个错误 “System.FormatException”类型的未经处理的异常在 System.IdentityModel.d ...

  3. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...

  4. 关于SubSonic3.0插件使用SqlQuery或Select查询时产生的System.NullReferenceException异常修复

    早上在编写执行用例时,突然爆异常System.NullReferenceException: 未将对象引用设置到对象的实例 执行代码:

  5. 关于SubSonic3.0查询或更新时出现System.NullReferenceException异常的处理

    在调试程序时,同事发现添加记录时,出现了System.NullReferenceException异常 DictBase dict = new DictBase();    dict.DictCode ...

  6. 关于System.TypeInitializationException异常

    什么是System.TypeInitializationException 作为类初始值设定项引发的异常的包装器而引发的异常. 继承 Object Exception SystemException ...

  7. “System.FormatException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理 其他信息: 该字符串未被识别为有效的 DateTime。

    前台用过jquery ajax将值传递到 后台的一般处理程序 并且报了这个异常 原因是:前台传递过来的时间格式不对  需要把 "/"换成 "-"   例如:20 ...

  8. .Net中使用com组件后发生System.ArithmeticException异常的解决办法(Message=算术运算中发生溢出或下溢。)

    最近在开发一个.Net程序,其中涉及到对com组件的调用,或者第三方DLL调用, 在调用完以后如果使用一些小的测试程序继续运行,一切正常,但是在使用带有GUI的form程序,或者WPF程序中,继续执行 ...

  9. [.Net]System.OutOfMemoryException异常

    1. 一个异常情景 加载15000条等高线,平均每条线有400个点到三维球上,等待时间太长.而且可能会报内存异常. 2. 不错的分析 http://wenku.baidu.com/view/14471 ...

随机推荐

  1. SpringBoot第十五篇:swagger构建优雅文档

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11007470.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前面的十四 ...

  2. wifi串口服务器

    下面与大家分享上海卓岚无线wifi串口服务器ZLAN7104创建虚拟串口的设置使用心得 一.7104网线连接计算机,用ZLVircom即可搜索并配置 其中,串口设置需要匹配实际所接的串口设备,配置为相 ...

  3. Unity Shader 序列帧动画

    shader中的序列帧动画属于纹理动画中的一种,主要原理是将给定的纹理进行等分,再根据时间的变化循环播放等分中的一部分. Unity Shader 内置时间变量 名称 类型 描述 _Time floa ...

  4. AngularJS简介与四大特征

    1.1 AngularJS简介 AngularJS  诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.A ...

  5. ant-design自定义FormItem--上传文件组件

    自定义上传组件,只需要在内部的值变化之后调用props中的onChange方法就可以托管在From组件中, 此外为了保证,初始化值发生变化后组件也发生变化,需要检测initialValue 变化,这是 ...

  6. QT 随笔目录

    [1]基础部分 <信号和槽机制> <信号与槽知识点> <QString 与 string转换> <QT 继承QWidget && 继承QDia ...

  7. scrapy 使用

    启动方式: 写一个启动文件,与配置文件同级   from scrapy.cmdline import execute import sys,os sys.path.append(os.path.dir ...

  8. 折腾linux随笔 之 关闭Budgie默认自动隐藏应用的菜单栏 与 Gnome系桌面应用菜单无内容解决

    关闭Budgie默认自动隐藏应用菜单栏 首选项 -> 设置 -> 通用辅助功能 -> 打开 始终显示通用辅助菜单 后的开关 -> 注销桌面重新登录. done. 解决Gnome ...

  9. golang 学习笔记 ---new()和 make()的区别详解

    概述 Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似.不过解释两者之间的不同也非常容易. new 的主要特性 首先 new 是内建函数,你可以从 http://gol ...

  10. WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox

    原文:WPF 高级篇 MVVM (MVVMlight) 依赖注入使用Messagebox MVVMlight 实现依赖注入 把弹框功能 和接口功能注入到各个插件中 使用依赖注入 先把所有的ViewMo ...