被OverflowException击中 [英] Got hit by an OverflowException
本文介绍了被OverflowException击中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在最后一行下面的方法"中,我总是遇到异常:
In the Method below on the last line I'm always getting an exception:
System.OverflowException: Value was either too large or too small for an Int32.
我无法真正解释原因,因为我正在对此进行明确检查:
I can't really explain why because I'm checking explicitly for that:
private Int32 ConvertValue(double value)
{
if (value > Int32.MaxValue)
{
Console.WriteLine("Couldn't convert value " + value + " to Int32");
return Int32.MaxValue;
}
else if (value < Int32.MinValue)
{
Console.WriteLine("Couldn't convert value " + value + " to Int32");
return Int32.MinValue;
}
else
{
return Convert.ToInt32(value);
}
}
推荐答案
还要检查 double.IsNaN(value)
.
与 NaN
的比较始终得出false.
Compares with NaN
always yield false.
这篇关于被OverflowException击中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文