被OverflowException击中 [英] Got hit by an OverflowException

查看:47
本文介绍了被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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆