"FormatException" 当 ToInt32() 方法的参数类型无效时不会抛出
"FormatException" not thrown when ToInt32() method has invalid argument type
1)
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32( "a" );
Console.Write(a);
}
}
我收到 FormatException
消息:Input string was not in a correct format
。这很好理解。
2)
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32( Console.Read() );
Console.Write(a);
}
}
在第二种情况下,我可以输入任何字符,例如 abc
并显示在控制台中。
问题:为什么在第二种情况下不抛出 FormatException
以及为什么它能成功处理非 int
字符?
更新
使用 Console.ReadLine()
方法,returns string
类型,也没有被 FormatException
并在控制台成功打印任何字符。
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32(Console.ReadLine());
Console.Write(a);
}
}
因为Console.Read()
的输出是int。这意味着它得到了你输入的 int 表示,所以如果你输入字符,它实际上得到了那个字符的 int 表示,一切正常。
要查看详细情况:
int a;
a = Convert.ToInt32(Console.Read()); //input for example: abc
Console.WriteLine(a); //97
Console.WriteLine((char)a); //a
Return Value Type: System.Int32 The next character from the input
stream, or negative one (-1) if there are currently no more characters
to be read.
public static int Read()
FormatException : 值不包含后跟数字序列(0 到 9)的可选符号。
return type of Console.Read()
is an int
.
然后您调用 Convert.ToInt32(int)
:
Returns the specified 32-bit signed integer; no actual conversion is performed.
我强烈怀疑你在混合使用Console.ReadLine
和Console.Read
方法。
来自 Console.Read
doc;
Reads the next character from the standard input stream.
和
Return Value Type: System.Int32 The next character from the input
stream, or negative one (-1) if there are currently no more characters
to be read.
这意味着当你用这种方法输入 abc
时它 returns 97
(because 97
is the ascii value of the first character) 这是一个有效的整数。
https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx
ToInt32 确实有一个可以接受字符串的重载版本,但字符串必须是实数的表示; "a" 不是数字,但如果你有“101”,它会正确解析-
int a;
a = Convert.ToInt32("101"); //will parse to int
Console.Write(a);
a = Convert.ToInt32("a"); //will not parse to int
Console.Write(a);
第二个示例有效而第一个示例无效的原因是 Console.Read returns integer 值基于传递的下一个字符进入它,所以当你用它调用 ToInt32 时一切都很好。
更新-
刚刚也用ReadLine测试了一下,还是报错
1)
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32( "a" );
Console.Write(a);
}
}
我收到 FormatException
消息:Input string was not in a correct format
。这很好理解。
2)
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32( Console.Read() );
Console.Write(a);
}
}
在第二种情况下,我可以输入任何字符,例如 abc
并显示在控制台中。
问题:为什么在第二种情况下不抛出 FormatException
以及为什么它能成功处理非 int
字符?
更新
使用 Console.ReadLine()
方法,returns string
类型,也没有被 FormatException
并在控制台成功打印任何字符。
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32(Console.ReadLine());
Console.Write(a);
}
}
因为Console.Read()
的输出是int。这意味着它得到了你输入的 int 表示,所以如果你输入字符,它实际上得到了那个字符的 int 表示,一切正常。
要查看详细情况:
int a;
a = Convert.ToInt32(Console.Read()); //input for example: abc
Console.WriteLine(a); //97
Console.WriteLine((char)a); //a
Return Value Type: System.Int32 The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.
public static int Read()
FormatException : 值不包含后跟数字序列(0 到 9)的可选符号。
return type of Console.Read()
is an int
.
然后您调用 Convert.ToInt32(int)
:
Returns the specified 32-bit signed integer; no actual conversion is performed.
我强烈怀疑你在混合使用Console.ReadLine
和Console.Read
方法。
来自 Console.Read
doc;
Reads the next character from the standard input stream.
和
Return Value Type: System.Int32 The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.
这意味着当你用这种方法输入 abc
时它 returns 97
(because 97
is the ascii value of the first character) 这是一个有效的整数。
https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx
ToInt32 确实有一个可以接受字符串的重载版本,但字符串必须是实数的表示; "a" 不是数字,但如果你有“101”,它会正确解析-
int a;
a = Convert.ToInt32("101"); //will parse to int
Console.Write(a);
a = Convert.ToInt32("a"); //will not parse to int
Console.Write(a);
第二个示例有效而第一个示例无效的原因是 Console.Read returns integer 值基于传递的下一个字符进入它,所以当你用它调用 ToInt32 时一切都很好。
更新-
刚刚也用ReadLine测试了一下,还是报错