什么是OverflowException

就是溢出异常。这个一般是当在线程检查的上下文中执行的算术、强制转换或转换运算导致溢出时引发的异常。

继承
Object
Exception
SystemException
ArithmeticException
OverflowException

说明

  • OverflowException
    int value = 780000000;
    checked {
    try {
       // Square the original value.
       int square = value * value; 
       Console.WriteLine("{0} ^ 2 = {1}", value, square);
    }
    catch (OverflowException) {
       double square = Math.Pow(value, 2);
       Console.WriteLine("Exception: {0} > {1:E}.", 
                         square, Int32.MaxValue);
    } }
    // The example displays the following output:
    //       Exception: 6.084E+17 > 2.147484E+009.

  • byte value = 241;
    checked {
    try {
       sbyte newValue = (sbyte) value;
       Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", 
                         value.GetType().Name, value, 
                         newValue.GetType().Name, newValue);
    }
    catch (OverflowException) {
       Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
    } }                            
    // The example displays the following output:
    //       Exception: 241 > 127.

相关文章:

  • 2021-04-09
  • 2021-04-11
  • 2021-07-05
  • 2021-04-11
  • 2021-06-25
  • 2022-02-20
  • 2021-06-19
  • 2022-02-22
猜你喜欢
  • 2021-10-30
  • 2021-08-31
  • 2021-08-03
相关资源
相似解决方案