C# 中的 FormatException
当参数的格式无效时,将抛出 FomatException。
我们来看一个示例。
当我们将 int.Parse() 方法的设置值设为非 int 时,将抛出 FormatException,如下所示 −
示例
using System;
class Demo {
static void Main() {
string str = "3.5";
int res = int.Parse(str);
}
}由于我们传递的值是非整数,因此在编译上述程序时将抛出以下错误。
输出
Unhandled Exception: System.FormatException: Input string was not in a correct format.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP