BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.ArithmeticException System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates an error that causes a exception.

`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

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

An is thrown at run time under the following conditions:

An arithmetic operation produces a result that is outside the range of the data type returned by the operation. The following example illustrates the that is thrown by a multiplication operation that overflows the bounds of the type.

code reference: System.OverflowException#1

A casting or conversion operation attempts to perform a narrowing conversion, and the value of the source data type is outside the range of the target data type. The following example illustrates the that is thrown by the attempt to convert a large unsigned byte value to a signed byte value.

code reference: System.OverflowException#2

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 , 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 :

add.ovf.<signed>

conv.ovf.<to type>

conv.ovf.<to type>.un

mul.ovf.<type>

sub.ovf.<type>

newarr

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

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

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

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The following table shows the initial property values for an instance of .

  Property

  Value

  A null reference (Nothing in Visual Basic).

  The localized error message string.

  Initializes a new instance of the class.

  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The content of the should be understandable to the user. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

  The following table shows the initial property values for an instance of .

  Property

  Value

  A null reference (Nothing in Visual Basic).

  The error message string.

  Initializes a new instance of the class with a specified error message.

  The message that describes the error.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor is called during deserialization to reconstitute the exception object transmitted over a stream.

  Initializes a new instance of the class with serialized data.

  The object that holds the serialized object data.
  The contextual information about the source or destination.
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  An exception that is thrown as a direct result of a previous exception can include a reference to the previous exception in the property. The property returns the same value that is passed into the constructor, or a null reference (Nothing in Visual Basic) if the property does not supply the inner exception value to the constructor.

  The following table shows the initial property values for an instance of .

  Property

  Value

  The inner exception reference.

  The error message string.

  Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.

  The error message that explains the reason for the exception.
  The exception that is the cause of the current exception. If the parameter is not a null reference (Nothing in Visual Basic), the current exception is raised in a catch block that handles the inner exception.
  0
0