OverflowException 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當算術、鑄造或轉換操作在已檢查的上下文中導致溢位時,拋出的例外。
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
- 繼承
- 繼承
- 屬性
備註
在執行時,拋出 An 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 了嘗試將一個大型無符號位元組值轉換為有號位元組值時所拋出的 that。
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.<打字>.unmul.ovf.<類型>sub.ovf.<類型>newarr
OverflowException 使用 HRESULT COR_E_OVERFLOW,其值為 0x80131516。
如需查看 OverflowException 實例的初始屬性值列表,請參閱 OverflowException 的建構子。
建構函式
| 名稱 | Description |
|---|---|
| OverflowException() |
初始化 OverflowException 類別的新執行個體。 |
| OverflowException(SerializationInfo, StreamingContext) |
使用串行化數據,初始化 OverflowException 類別的新實例。 |
| OverflowException(String, Exception) |
初始化類別的新實例 OverflowException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。 |
| OverflowException(String) |
使用指定的錯誤訊息,初始化 OverflowException 類別的新實例。 |
屬性
| 名稱 | Description |
|---|---|
| Data |
取得索引鍵/值組的集合,提供例外狀況的其他使用者定義資訊。 (繼承來源 Exception) |
| HelpLink |
取得或設定與這個例外狀況相關聯的說明檔連結。 (繼承來源 Exception) |
| HResult |
取得或設定 HRESULT,這是指派給特定例外狀況的編碼數值。 (繼承來源 Exception) |
| InnerException |
會取得 Exception 造成目前例外的實例。 (繼承來源 Exception) |
| Message |
取得描述目前例外狀況的訊息。 (繼承來源 Exception) |
| Source |
取得或設定造成錯誤之應用程式或物件的名稱。 (繼承來源 Exception) |
| StackTrace |
取得呼叫堆疊上即時框架的字串表示。 (繼承來源 Exception) |
| TargetSite |
取得擲回目前例外狀況的方法。 (繼承來源 Exception) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的 物件是否等於目前的物件。 (繼承來源 Object) |
| GetBaseException() |
當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。 (繼承來源 Exception) |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
在衍生類別中覆寫時,使用例外狀況的相關信息來設定 SerializationInfo。 (繼承來源 Exception) |
| GetType() |
取得目前實例的運行時間類型。 (繼承來源 Exception) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
建立並傳回目前例外狀況的字串表示。 (繼承來源 Exception) |
事件
| 名稱 | Description |
|---|---|
| SerializeObjectState |
發生於例外狀況串行化以建立例外狀況狀態物件,其中包含例外狀況的串行化數據。 (繼承來源 Exception) |