OverflowException 類別

定義

當檢查內容中的算術、轉型或轉換作業造成溢位時,所擲回的例外狀況。

public ref class OverflowException : ArithmeticException
public class OverflowException : ArithmeticException
[System.Serializable]
public class OverflowException : ArithmeticException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class OverflowException : ArithmeticException
type OverflowException = class
    inherit ArithmeticException
[<System.Serializable>]
type OverflowException = class
    inherit ArithmeticException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OverflowException = class
    inherit ArithmeticException
Public Class OverflowException
Inherits ArithmeticException
繼承
繼承
屬性

備註

OverflowException在執行時間在下列情況下擲回 :

  • 算數運算會產生超出作業所傳回之資料類型範圍的結果。 下列範例說明 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.
    
    open Checked
    
    let v = 780000000
    try
       // Square the original value.
       let square = v * v
       printfn $"{v} ^ 2 = {square}"
    with :? OverflowException ->
        let square = float v ** 2
        printfn $"Exception: {square} > {Int32.MaxValue:E}."
    // The example displays the following output:
    //       Exception: 6.084E+17 > 2.147484E+009.
    
    Dim value As Integer = 780000000
    Try
       ' Square the original value.
       Dim square As Integer = value * value 
       Console.WriteLine("{0} ^ 2 = {1}", value, square)
    Catch e As OverflowException
       Dim square As Double = Math.Pow(value, 2)
       Console.WriteLine("Exception: {0} > {1:E}.", _
                         square, Int32.MaxValue)
    End Try
    ' 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.
    
    open Checked
    
    let value = 241uy
    try
        let newValue = int8 value
        printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
    with :? OverflowException ->
        printfn $"Exception: {value} > {SByte.MaxValue}."
    // The example displays the following output:
    //       Exception: 241 > 127.
    
    Dim value As Byte = 241
    Try
       Dim newValue As SByte = (CSByte(value))
       Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
                         value.GetType().Name, value, _
                         newValue.GetType().Name, newValue)
    Catch e As OverflowException
       Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue)
    End Try                            
    ' The example displays the following output:
    '       Exception: 241 > 127.
    

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

若要讓算術、轉型或轉換作業擲 OverflowException 回 ,此作業必須在檢查的內容中發生。 根據預設,會檢查 Visual Basic 中的算數運算和溢位;在 C# 和 F# 中,它們不是 。 如果作業發生在未核取的內容中,則會捨棄任何不符合目的地類型的高序位來截斷結果。 下列範例說明 C# 或 F# 中的這類未核取轉換。 它會在未核取的內容中重複上述範例。

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.
let value = 241uy
try
    let newValue = int8 value
    printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
with :? OverflowException ->
    printfn $"Exception: {value} > {SByte.MaxValue}."
// The example displays the following output:
//       Converted the Byte value 241 to the SByte value -15.

下列Microsoft中繼語言 (MSIL) 指示會擲回 OverflowException

  • add.ovf.<簽署>

  • conv.ovf.<至 類型>

  • conv.ovf.<至 類型>.un

  • mul.ovf.<類型>

  • sub.ovf.<類型>

  • newarr

OverflowException 會使用 HRESULT COR_E_OVERFLOW,其值為 0x80131516。

如需執行個體的初始屬性值的清單OverflowException,請參閱OverflowException建構函式。

建構函式

OverflowException()

初始化 OverflowException 類別的新執行個體。

OverflowException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 OverflowException 類別的新執行個體。

OverflowException(String)

使用指定的錯誤訊息,初始化 OverflowException 類別的新執行個體。

OverflowException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 OverflowException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱