C# | How to use multiple catch clause

The main purpose of the catch block is to handle the exception raised in the try block. This block is only going to execute when the exception raised in the program.
In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception. If you use multiple catch blocks for the same type of exception, then it will give you a compile-time error because C# does not allow you to use multiple catch block for the same type of exception. A catch block is always preceded by the try block.

In general, the catch block is checked within the order in which they have occurred in the program. If the given type of exception is matched with the first catch block, then first catch block executes and the remaining of the catch blocks are ignored. And if the starting catch block is not suitable for the exception type, then compiler search for the next catch block.

Syntax:

try {

// Your code 

}

// 1st catch block
catch(Exception_Name) {

// Code

}

// 2nd catch block
catch(Exception_Name) {

// Code

}

.
.
.
.

Below given are some examples to understand the implementation in a better way:

Example 1: In the below example, try block generate two different types of exception i.e DivideByZeroException and IndexOutOfRangeException. Now we use two catch blocks to handle these exceptions that are associated with a single try block. Each catch block caught a different type of exception like catch block 1 is used to catch DivideByZeroException, catch block 2 is used to catch IndexOutOfRangeException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// use of multiple catch block
using System;
  
class GFG {
  
    // Main Method
    static void Main()
    {
  
        // Here, number is greater than divisor
        int[] number = { 8, 17, 24, 5, 25 };
        int[] divisor = { 2, 0, 0, 5 };
  
        // --------- try block ---------
  
        for (int j = 0; j < number.Length; j++)
  
            // Here this block raises two different
            // types of exception, i.e. DivideByZeroException
            // and IndexOutOfRangeException
            try {
  
                Console.WriteLine("Number: " + number[j]);
                Console.WriteLine("Divisor: " + divisor[j]);
                Console.WriteLine("Quotient: " + number[j] / divisor[j]);
            }
  
            // Catch block 1
  
            // This Catch block is for
            // handling DivideByZeroException
            catch (DivideByZeroException) {
  
                Console.WriteLine("Not possible to Divide by zero");
            }
  
            // Catch block 2
  
            // This Catch block is for
            // handling IndexOutOfRangeException
            catch (IndexOutOfRangeException) {
                Console.WriteLine("Index is Out of Range");
            }
    }
}

chevron_right


Output:

Number: 8
Divisor: 2
Quotient: 4
Number: 17
Divisor: 0
Not possible to Divide by zero
Number: 24
Divisor: 0
Not possible to Divide by zero
Number: 5
Divisor: 5
Quotient: 1
Number: 25
Index is Out of Range

Example 2: In the below example, try block raise an exception. So we will use three different type of catch blocks to handle the exception raised by the try block. Catch block 1 will handle IndexOutOfRangeException, catch block 2 will handle FormatException, and catch block 3 will handle OverflowException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the concept
// of multiple catch clause
using System;
  
class GFG {
  
    // Main method
    static void Main()
    {
  
        // This block raises an exception
        try {
  
            byte data = byte.Parse("a");
            Console.WriteLine(data);
        }
  
        // Catch block 1
  
        // This block is used to handle
        // IndexOutOfRangeException type exception
        catch (IndexOutOfRangeException) {
  
            Console.WriteLine("At least provide one Argument!");
        }
  
        // Catch block 2
  
        // This block is used to handle
        // FormatException type exception
        catch (FormatException) {
  
            Console.WriteLine("Entered value in not a number!");
        }
  
        // Catch block 3
  
        // This block is used to handle
        // OverflowException type exception
        catch (OverflowException) {
  
            Console.WriteLine("Data is out of Range!");
        }
    }
}

chevron_right


Output:

Entered value in not a number!


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.