Convert long to integer and catch the OverflowException in CSharp

Description

The following code shows how to convert long to integer and catch the OverflowException.

Example


using System;//w  ww .ja v a2  s  . c  o m
using System.Globalization;

public class Example
{
    public static void Main()
    {
        long lNumber = 99111111111111111L;
        try
        {
            int number1 = (int)lNumber;
            Console.WriteLine(number1);
        }
        catch (OverflowException)
        {
            Console.WriteLine("{0} is out of range of an Int32.", lNumber);
        }
    }
}

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