Saturday 2 December 2017

C# Program to Illustrate Exception Handling for Invalid TypeCasting in UnBoxing


Code:

class TestUnboxing
{
    static void Main()
    {
        int num = 123;
        object obj = num; 
        try
        {
            int j = (short)obj; 
            System.Console.WriteLine("Unboxing");
        }
        catch (System.InvalidCastException e)
        {
            System.Console.WriteLine("{0} Error: Incorrect unboxing", e.Message);
        }
        System.Console.Read();
    }
}


Output:

Specified Cast is not Valid.Error : Incorrect Boxing


More C# Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...