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
继承
继承
属性

注解

在运行时,在以下条件下引发 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 了尝试将大型无符号字节值转换为有符号字节值所引发的值。

    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. <signed>

  • conv.ovf. <to type>

  • conv.ovf. <to type> .un

  • mul.ovf. <type>

  • sub.ovf. <type>

  • newarr

OverflowException 使用具有值0x80131516的 HRESULT COR_E_OVERFLOW。

有关实例的初始属性值的列表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)

适用于

另请参阅