C# - Exception Handling



What Is an Exception?

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another.

Exception Handling in C#

C# exception handling is built upon four keywords: try, catch, finally, and throw:

  • try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

  • catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

  • finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

  • throw − A program throws an exception when a problem shows up. This is done using a throw keyword.

Exception Handling Basic Structure (try-catch, finally)

Assuming a block raises an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −

try {
   // statements causing exception
} catch( ExceptionName e1 ) {
   // error handling code
} catch( ExceptionName e2 ) {
   // error handling code
} catch( ExceptionName eN ) {
   // error handling code
} finally {
   // statements to be executed
}

You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations.

Exception Classes in C#

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.

The System.ApplicationException class supports exceptions generated by application programs. Hence the exceptions defined by the programmers should derive from this class.

The System.SystemException class is the base class for all predefined system exception.

The following table provides some of the predefined exception classes derived from the Sytem.SystemException class −

Sr.No. Exception Class & Description
1

System.IO.IOException

Handles I/O errors.

2

System.IndexOutOfRangeException

Handles errors generated when a method refers to an array index out of range.

3

System.ArrayTypeMismatchException

Handles errors generated when type is mismatched with the array type.

4

System.NullReferenceException

Handles errors generated from referencing a null object.

5

System.DivideByZeroException

Handles errors generated from dividing a dividend with zero.

6

System.InvalidCastException

Handles errors generated during typecasting.

7

System.OutOfMemoryException

Handles errors generated from insufficient free memory.

8

System.StackOverflowException

Handles errors generated from stack overflow.

Handling Exceptions

C# provides a structured solution to the exception handling in the form of try and catch blocks. Using these blocks the core program statements are separated from the error-handling statements.

These error handling blocks are implemented using the try, catch, and finally keywords.

Example

Following is an example of throwing an exception when dividing by zero condition occurs −

using System;
namespace ErrorHandlingApplication {
   class DivNumbers {
      int result;
      
      DivNumbers() {
         result = 0;
      }
      public void division(int num1, int num2) {
         try {
            result = num1 / num2;
         } catch (DivideByZeroException e) {
            Console.WriteLine("Exception caught: {0}", e);
         } finally {
            Console.WriteLine("Result: {0}", result);
         }
      }
      static void Main(string[] args) {
         DivNumbers d = new DivNumbers();
         d.division(25, 0);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Exception caught: System.DivideByZeroException: Attempted to divide by zero. 
at ...
Result: 0

Creating User-Defined Exceptions

You can also define your own exception. User-defined exception classes are derived from the Exception class.

Example

The following example demonstrates this −

using System;
namespace UserDefinedException {
   class TestTemperature {
      static void Main(string[] args) {
         Temperature temp = new Temperature();
         try {
            temp.showTemp();
         } catch(TempIsZeroException e) {
            Console.WriteLine("TempIsZeroException: {0}", e.Message);
         }
         Console.ReadKey();
      }
   }
}
public class TempIsZeroException: Exception {
   public TempIsZeroException(string message): base(message) {
   }
}
public class Temperature {
   int temperature = 0;
   
   public void showTemp() {
      
      if(temperature == 0) {
         throw (new TempIsZeroException("Zero Temperature found"));
      } else {
         Console.WriteLine("Temperature: {0}", temperature);
      }
   }
}

When the above code is compiled and executed, it produces the following result −

TempIsZeroException: Zero Temperature found

Throwing Objects

You can throw an object if it is either directly or indirectly derived from the System.Exception class. You can use a throw statement in the catch block to throw the present object as −

Catch(Exception e) {
   ...
   Throw e
}

Example

In the following example, we are demonstrating how to catch an exception and re-throw the same exception object:

using System;

class Program
{
    static void Main()
    {
        try
        {
            DivideNumbers(10, 0);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught in Main:");
            Console.WriteLine(e.Message);
        }
    }

    static void DivideNumbers(int a, int b)
    {
        try
        {
            int result = a / b;
            Console.WriteLine("Result: " + result);
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("Caught inside DivideNumbers method.");
            // Re-throwing the same exception object
            throw ex;
        }
    }
}

When the above code is compiled and executed, it produces the following result −

Caught inside DivideNumbers method.
Exception caught in Main:
Attempted to divide by zero.

Multiple catch Blocks

With a single try block, you can use multiple catch blocks to handle specific exceptions.

Example

The following example demonstrates how multiple 'catch' blocks can be used to handle different types of exceptions:

using System;

class Program
{
    static void Main()
    {
        try
        {
            int[] numbers = { 1, 2, 3 };
            Console.WriteLine("Accessing element: " + numbers[5]);
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("DivideByZeroException caught: " + ex.Message);
        }
        catch (IndexOutOfRangeException ex)
        {
            Console.WriteLine("IndexOutOfRangeException caught: " + ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("General Exception caught: " + ex.Message);
        }
    }
}

When the above code is compiled and executed, it produces the following result −

IndexOutOfRangeException caught: Index was outside the bounds of the array.

Nested try-catch Blocks

Nesting of try-catch blocks are allowed in C#. You can use try-catch blocks inside other try blocks to handle complex workflows.

Example

The following example demonstrates how to use nested try-catch blocks in C#:

using System;

class Program
{
    static void Main()
    {
        try
        {
            try
            {
                int x = 0;
                int result = 10 / x;
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Inner catch: " + ex.Message);
                throw; // Rethrow the same exception
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Outer catch: " + ex.Message);
        }
    }
}

When the above code is compiled and executed, it produces the following result −

Inner catch: Attempted to divide by zero.
Outer catch: Attempted to divide by zero.

Finally Block

If you need to write code that should always execute, whether an exception occurs or the code is successfully executed, you can use the finally block.
The finally block always executes. It can be used to release resources such as closing files, database connections, etc.

Example

The following example demonstrates the use of a finally block:

using System;

class Program
{
    static void Main()
    {
        try
        {
            Console.WriteLine("Inside try block.");
            int result = 10 / 2; // Change to 0 to force exception
            Console.WriteLine("Result: " + result);
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("Exception caught: " + ex.Message);
        }
        finally
        {
            Console.WriteLine("Finally block executed.");
        }
    }
}

When the above code is compiled and executed, it produces the following result −

Inside try block.
Result: 5
Finally block executed.
Advertisements