C# Program that Demonstrates Exception Handling For Invalid TypeCasting in UnBoxing - GeeksforGeeks

01 Nov, 2021
Exception handling is used to handle the errors in the program. Or we can say that an exception is an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of the occurrence of an exception are not known to the program. In this situation, we can create an exception object and call the exception handler code. This exception handler saves the program from the crash and this process is known as exception handling. Exception handling is necessary because it handles an unwanted event so that the program code still makes sense to the user. We can achieve this by using try and catch block
try: This block identifies a block of code for which any errors are there. If there is any error then it will send to catch block. It can contain one or more catch block and this block is created using try keyword. catch: This block is used to handle the error raised in the try block. The catch keyword is used for catch block. For the catch block, there should be definitely a try block.finally: This block is used to run the specified set of statements, whether an exception is thrown or not thrown. It is an optional block and created by using the finally keyword.
try: This block identifies a block of code for which any errors are there. If there is any error then it will send to catch block. It can contain one or more catch block and this block is created using try keyword. 
catch: This block is used to handle the error raised in the try block. The catch keyword is used for catch block. For the catch block, there should be definitely a try block.
finally: This block is used to run the specified set of statements, whether an exception is thrown or not thrown. It is an optional block and created by using the finally keyword.
Syntax:
try{
      // Statements
}
catch{
     // Handle the exception
}
finally{
    // Finally block
}
Typecasting can be defined as the process of converting the variable from one data type to another. If the data types are compatible, then C# does Automatic Type Conversion. If not comparable, then they need to be converted explicitly which is known as Explicit Type conversion.
Syntax:
(datatype)variable/object;
Here, the data type is the type of data that we type case, For example, we can convert Integer type to Short type. 
Here in this article, we have to handle the Typecasting integer to short data type and we are going to handle that exception.
Example:
Input  : 50
Output : Specified cast is not valid.
Approach:
Declare an integer variable named “number”.Convert that variable to the object(This is known as Unboxing).Convert the integer variable into short data type in try block.Handle the exception in catch block.
Declare an integer variable named “number”.
Convert that variable to the object(This is known as Unboxing).
Convert the integer variable into short data type in try block.
Handle the exception in catch block.
Example:
C#
// C# program to illustrate how to exception handling // for invalid TypeCasting in unBoxingusing System;class GFG{      static void Main(){          // Declare a number    int number = 50;          // Set this number to the object    object object1 = number;     try    {                  // Type cast this object to short        int x = (short)object1;         System.Console.WriteLine("Unboxing the object");    }          // Handle exception    catch (System.InvalidCastException e)    {                  // Display the error message        System.Console.WriteLine(e.Message);    }}}
Output:
Specified cast is not valid.
CSharp-Exception-Handling
CSharp-programs
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Partial Classes in C#
C# | Inheritance
C# | List Class
Difference between Hashtable and Dictionary in C#
Lambda Expressions in C#