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.