OverflowException 會在下列情況下執行階段擲回︰

  • Int32 型別。

    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.
    
  • OverflowException 所擲回的嘗試將大型的不帶正負號的位元組值轉換為帶正負號的位元組值。

     
    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.
    

在每個案例中,運算的結果,是值小於 MinValue 屬性或大於 MaxValue 作業所產生的資料型別屬性。

它會重複先前的範例中未檢查的內容。

 
byte value = 241;
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:
//       Converted the Byte value 241 to the SByte value -15.

摘自:https://msdn.microsoft.com/zh-tw/library/system.overflowexception(v=vs.110).aspx

相关文章:

  • 2022-12-23
  • 2022-02-08
  • 2021-09-23
  • 2021-10-12
  • 2021-10-29
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-22
  • 2022-01-20
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案