OverflowException 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当检查的上下文中的算术、强制转换或转换操作导致溢出时引发的异常。
public ref class OverflowException : ArithmeticExceptionpublic class OverflowException : ArithmeticException[System.Serializable]
public class OverflowException : ArithmeticException[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class OverflowException : ArithmeticExceptiontype OverflowException = class
    inherit ArithmeticException[<System.Serializable>]
type OverflowException = class
    inherit ArithmeticException[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OverflowException = class
    inherit ArithmeticExceptionPublic 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.<>
- 要键入 - 的 
- 要键入 - 的 
- mul.ovf.<类型>
- sub.ovf.<类型>
- 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) |