Specified Cast is not Valid.Error : Incorrect Boxing
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Demonstrate Exception Handling for Invalid TypeCasting in UnBoxing
C# Program to Demonstrate Exception Handling for Invalid TypeCasting in UnBoxing
This is a C# Program to illustrate exception handling for invalid typeCasting in unboxing.
This C# Program Illustrates Exception Handling for Invalid TypeCasting in UnBoxing.
Here it demonstrates a case of invalid unboxing and the resulting InvalidCastException. Using try and catch, an error message is displayed when the error occurs.
Here is source code of the C# Program to Illustrate Exception Handling for Invalid TypeCasting in UnBoxing. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Illustrate Exception Handling for Invalid TypeCasting in UnBoxing */ 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(); } }
This C# program is used to illustrate exception handling for invalid TypeCasting in UnBoxing. Demonstrate a case of invalid unboxing and the resulting InvalidCastException. Using try and catch, an error message is displayed when the error occurs.