System.OverflowException Class
Represents the error that occurs when the result of an arithmetic operation is too large to be represented by the destination type.

See Also: OverflowException Members

Syntax

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

Remarks

In languages that detect overflow, this is the exception that gets thrown. For example, in C#, the checked keyword is used to detect overflow conditions. A OverflowException exception occurs only in a checked context.

Note:

The following CIL instructions throw OverflowException :

  • add.ovf.<signed>
  • conv.ovf.<to type>
  • conv.ovf.<to type>.un
  • mul.ovf.<type>
  • sub.ovf.<type>
  • newarr

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