System.OverflowException Class

The exception that is thrown when an arithmetic, casting, or conversion operation in a checked context results in an overflow.

See Also: OverflowException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class OverflowException : ArithmeticException

Remarks

An OverflowException is thrown at run time under the following conditions:

In each case, the result of the operation is a value that is less than the MinValue property or greater than the MaxValue property of the data type that results from the operation.

For the arithmetic, casting, or conversion operation to throw an OverflowException, the operation must occur in a checked context. By default, arithmetic operations and overflows in Visual Basic are checked; in C#, they are not. If the operation occurs in an unchecked context, the result is truncated by discarding any high-order bits that do not fit into the destination type. The following example illustrates such an unchecked conversion in C#. It repeats the previous example in an unchecked context.

code reference: System.OverflowException#3

The following Microsoft intermediate language (MSIL) instructions throw an OverflowException:

OverflowException uses the HRESULT COR_E_OVERFLOW, which has the value 0x80131516.

For a list of initial property values for an instance of OverflowException, see the OverflowException.#ctor constructors.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates an error that causes a OverflowException exception.

C# Example

using System;
public class OverflowExample {
   public static void Main() {
      int i = 400;
      byte b = 0;
      try {
         checked { b = (byte)( i ); }
      }
      catch ( OverflowException e ) {
         Console.WriteLine( "Error caught: {0}", e );
      }
   }
}
   

The output is

Example

Error caught: System.OverflowException: Arithmetic operation resulted in an overflow.
   at OverflowExample.Main()
 

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0