C# OverflowException

csharpprogrammingserver side programming

当参数值超出整数范围时,会抛出OverflowException。

让我们看一个例子。

当我们将一个超出整数范围的值设置为 int.Parse() 方法时,会抛出 OverflowException,如下所示 −

示例

using System;
class Demo {
   static void Main() {
      string str = "757657657657657";
      int res = int.Parse(str);
   }
}

输出

由于我们传递了一个超出整数(Int32)范围的值,因此编译上述程序时会引发以下错误。

Unhandled Exception:
System.OverflowException: Value was either too large or too small for an Int32.

相关文章