Catch OverflowException Exception for data overflow in CSharp

Description

The following code shows how to catch OverflowException Exception for data overflow.

Example


using System;/* w  w w.  j a  v  a  2  s .c om*/

class MainClass
{
    public static void Main()
    {
        try
        {
            checked
            {
                int Integer1;
                int Integer2;
                int Sum;

                Integer1 = 999999999;
                Integer2 = 2000000000;
                Sum = (int)(Integer1 + Integer2);
            }
        }
        catch (OverflowException)
        {
            Console.WriteLine("A mathematical operation caused an overflow.");
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var