11
OctException Handling in C#
Exception Handling in C#- An OverView
Explore the fundamentals of Exception Handling in C#, mastering the art of managing errors and ensuring code stability. Learn key concepts and best practices for handling exceptions effectively in this comprehensive guide.
What is Exception Handling in C#
Exception Handling in C# is a mechanism that manages and responds to errors during program execution. It enables code to gracefully handle unexpected situations, preventing crashes and allowing for controlled recovery, enhancing the reliability and robustness of the software.
Advantage
- Error Control: Allows precise identification and handling of errors, enhancing code reliability.
- Maintains Flow: Preserves program flow by segregating error handling from regular code.
- Debugging: Facilitates debugging by providing detailed information about exceptions.
- Robustness: Enhances software robustness, ensuring smoother execution and better user experience.
C# Exception Classes
Exception | Description |
System.DivideByZeroException | manages the mistake that results from dividing an integer by zero. |
System.NullReferenceException | manages the error that is produced when the null object is referenced. |
System.InvalidCastException | responds to the error that incorrect typecasting produces. |
System.IO.IOException | responds to input/output failures. |
System.FieldAccessException | addresses the problem caused by an unauthorized entry to a private or protected field. |
C# Exception Handling Keywords
In C#, we use 4 keywords to perform exception handling:
- try
- catch
- finally, and
- throw
Exception Handling in C# Using try-catch block
The try-catch block in C# is a structure used to handle exceptions. Code within the "try" block is tested for errors, and if an exception occurs, the "catch" block is triggered, allowing for specific handling and recovery from the error condition.
Example
using System;
class Program
{
static void Main()
{
try
{
// Divide by zero to trigger an exception
int number = 10;
int divisor = 0;
int result = number / divisor;
Console.WriteLine("Result: " + result); // This line won't execute
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception caught: " + ex.Message);
}
finally
{
Console.WriteLine("Finally block executed.");
}
}
}
Explanation
This code attempts to divide a number by zero, which triggers a DivideByZeroException. The catch block catches this exception, displaying a message, and the finally block is executed regardless of whether an exception occurred or not.
Output
Exception caught: Attempted to divide by zero.
Finally block executed.
Example of Using Multiple try-catch blocks
using System;
class Program
{
static void Main()
{
try
{
int[] numbers = { 10, 20, 30 };
Console.WriteLine("Enter index to retrieve number:");
int index = Convert.ToInt32(Console.ReadLine());
try
{
int result = numbers[index];
Console.WriteLine($"Number at index {index} is: {result}");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Index is out of range!");
}
}
catch (FormatException)
{
Console.WriteLine("Invalid input format!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Explanation
This code has nested try-catch blocks. The outer block catches FormatException if the user inputs an invalid format when entering the index. The inner block handles IndexOutOfRangeException if the index entered is out of range for the array. The last catch block handles any other unexpected exceptions.
Output
Enter index to retrieve number:
5
Index is out of range!
C# finally
In C#, the "finally" block is used to define code that executes regardless of whether an exception is thrown or not. It ensures essential cleanup tasks or actions occur, providing a consistent approach to finalize operations, even in error scenarios.
User Defined Exceptions
User Defined Exceptions in C# are custom exceptions created by developers to handle specific error scenarios not covered by built-in exceptions. They extend the base Exception class, allowing tailored error messaging and specialized handling for unique application requirements.
Example
using System;
public class InvalidAgeException : Exception
{
public InvalidAgeException(String message)
: base(message)
{
}
}
public class TestUserDefinedException
{
static void validate(int age)
{
if (age < 18)
{
throw new InvalidAgeException("Sorry, Age must be greater than 18");
}
}
public static void Main(string[] args)
{
try
{
validate(12);
}
catch (InvalidAgeException e) { Console.WriteLine(e); }
Console.WriteLine("Rest of the code");
}
}
Explanation
The code will throw an InvalidAgeException since the validate() method checks if the provided age is less than 18. In the Main method, the exception is caught, and the message "Sorry, Age must be greater than 18" will be displayed. Afterward, "Rest of the code" will be printed to the console.
Output
InvalidAgeException: Sorry, Age must be greater than 18
Rest of the code
C# Checked
The "checked" keyword in C# ensures that arithmetic operations check for overflow, providing explicit control over numeric computations. When applied, it enables runtime checks, throwing exceptions if overflow occurs, ensuring safer numerical manipulations within the program.
Example
using System;
class Program
{
static void Main()
{
int a = int.MaxValue;
int b = 2;
try
{
checked
{
int result = a * b;
Console.WriteLine("Result: " + result);
}
}
catch (OverflowException ex)
{
Console.WriteLine("Overflow Exception: " + ex.Message);
}
}
}
Explanation
In this example, int.MaxValue represents the maximum value an int can hold. Multiplying it by 2 will cause an overflow, triggering the OverflowException that is caught within the try-catch block when using the checked keyword.
Output
Overflow Exception: Arithmetic operation resulted in an overflow.
C# Unchecked
In C#, the "unchecked" keyword allows arithmetic operations to bypass overflow checking, enabling the compiler to skip runtime checks for integer arithmetic overflow. This keyword helps optimize performance by suppressing overflow exceptions but requires careful handling to ensure accurate calculations.
Example
using System;
class Program
{
static void Main()
{
int num1 = int.MaxValue; // Assigning maximum integer value
int num2 = 2;
unchecked
{
int result = num1 + num2; // Performing addition with unchecked keyword
Console.WriteLine($"Result without overflow check: {result}");
}
}
}
Explanation
In this example, int.MaxValue is the maximum value an int variable can hold. By adding 2 to this maximum value inside an unchecked block, it wraps around due to overflow and results in -2147483647.
Output
Result without overflow check: -2147483647
C# SystemException class
The SystemException class in C# is the base class for all predefined system exceptions. It encompasses exceptional conditions that derive from the CLR (Common Language Runtime) and serves as the root for most runtime exceptions, offering a foundational structure for handling and managing various error scenarios.
Conclusion
In C#, robust exception handling is vital for stable software. Properly managing exceptions ensures graceful error recovery and enhances program reliability, contributing to a smoother user experience and streamlined application performance.
Take our Csharp skill challenge to evaluate yourself!
In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.