C# Program that Demonstrates Exception Handling For Invalid TypeCasting in UnBoxing
Last Updated :
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.
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.
Example:
C#
// C# program to illustrate how to exception handling
// for invalid TypeCasting in unBoxing
using 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.
Similar Reads
Exception Handling in Programming Exception handling is a critical aspect of programming, enabling developers to manage unexpected or erroneous situations gracefully. In this article, we'll discuss the concept of exception handling, its importance, and best practices for implementing it effectively in various programming languages.
7 min read
Exception handling in Objective-C Exception handling is an essential aspect of Objective-C programming, enabling developers to manage unforeseen errors effectively. Objective-C provides a robust set of tools and methodologies to handle exceptions, ensuring the stability and reliability of applications. Let's delve deeper into the nu
5 min read
Error Handling in Programming In Programming, errors occur. Our code needs to be prepared for situations when unexpected input from users occurs, division by zero occurs, or a variable name is written incorrectly. To prevent unexpected events from crashing or having unexpected outcomes, error handling involves putting in place m
12 min read
How to detect Stack Unwinding in a Destructor in C++? What is Stack Unwinding? Stack unwinding is the process of executing destructors for all local objects when an exception propagates out of a function. It happens when an exception is thrown and not caught within the same function. When this occurs, the destructors for all objects with automatic stor
4 min read
How to Solve Class Cast Exceptions in Java? An unexcepted, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating at the U.S.A. At runtime, if the remote fi
3 min read