Exception Handling in C#


In this article we are going to discuss Exception Handling in C#. There are many blogs, articles are available on the internet regarding Exception Handling but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Exception Handling use in C#.

C# Exceptions

When executing C# code different errors can occur - coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

In C#, All exceptions the derived from System.Exception class. When an error occurs, C# will normally stop and generate an error message.

C# Exception Handling Keywords

In C#, we use four keywords to perform exception handling:

  1. try block - Used to define a try block. This block holds the code that may throw an exception.
  2. catch block - Used to define a catch block. This block catches the exception thrown by the try block.
  3. finally block - Used to define the finally block. This block holds the default code.
  4. throw Keyword - Used to throw an exception manually.

1. C# try and catch

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The try and catch keywords come in pairs:

Syntax

try 
{
  //  Block of code to try
}
catch (Exception e)
{
  //  Block of code to handle errors
}

In the following example, we use the variable inside the catch block (e) together with the built-in Message property, which outputs a message that describes the exception.

C# example without try/catch


class Program
{
    static void Main()
    {
        int a = 10;
        int b = 0;
        int x = a / b;
        Console.WriteLine("Rest of the code");
        Console.ReadKey();
    }
}

Output

Exception Handling in C#

In the above output, an error occurs, C# will normally stop and generate an error message. So it will not consider the following code line.

Console.WriteLine("Rest of the code");

C# example with try/catch

The following code defines the try catch block.

using System;
class Program
{
    static void Main()
    {
        try
        {
            int a = 10;
            int b = 0;
            int x = a / b;
        }
        catch (Exception e) 
        { 
            Console.WriteLine(e); 
        }
        Console.WriteLine("Rest of the code");
        Console.ReadKey();
    }
}

Output

Exception Handling in C#

In the above output, an error occurs, it will also execute following code line.

Console.WriteLine("Rest of the code");

C# Finally Block

Finally block is mostly used to dispose the unwanted objects when they are no more required. This is good for performance, otherwise you have to wait for garbage collector to dispose them.

C# finally block is used to execute important code which is to be executed whether exception is handled or not. It must be preceded by catch or try block. It is often used for cleanup actions.

Example

using System;
class Program
{
    static void Main()
    {
        try
        {
            int a = 10;
            int b = 0;
            int x = a / b;
        }
        catch (Exception e) 
        { 
            Console.WriteLine(e); 
        }
        finally 
        { 
            Console.WriteLine("The 'try catch' is finished. Finally block is executed"); 
        }
        Console.WriteLine("Rest of the code");
        Console.ReadKey();
    }
}

Output

Exception Handling in C#
Next