Exception Handling in C#

Exception Handling in C#

03 Jul 2024
Beginner
211 Views
8 min read
Learn via Video Course & by Doing Hands-on Labs

Free C# Course Online

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

ExceptionDescription
System.DivideByZeroExceptionmanages the mistake that results from dividing an integer by zero.
System.NullReferenceExceptionmanages the error that is produced when the null object is referenced.
System.InvalidCastExceptionresponds to the error that incorrect typecasting produces.
System.IO.IOExceptionresponds to input/output failures.
System.FieldAccessExceptionaddresses 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.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Full-Stack .NET Developer Certification TrainingOct 06SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this