C# | Nesting of try and catch blocks

In C#, the nesting of the try & catch block is allowed. The nesting of try block means one try block can be nested into another try block. The various programmer uses the outer try block to handling serious exceptions, whereas the inner block for handling normal exceptions.

Note:

  • If an exception raises in the inner try block that is not caught by the catch block associated with the try block, then the exception is promoted to the outer try block. Generally, nested try blocks are used to permit different groups of the error to be handled in different ways.
  • It is a necessary condition that a try block must be followed by a catch or finally blocks because if you use a try block without a catch or finally then you will tend to a compile-time error.

Syntax:

// outer try block
try
{

   // inner try block
   try
     {
       // code...
     }

   // inner catch block
   catch
     {
       // code...
     }
}

// outer catch block
catch
  {
    // code...
  }

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

Example 1: In this program, DivideByZeroException is generated within the inner try block that is caught by a catch block associated with the inner try block and continue the flow of the program. When IndexOutOfRangeException generates within the inner try block which is not caught by the inner catch block then inner try block transfer this exception to the outer try block. After that, the catch block associated with the outer try block caught the exception which causes the program to terminate. Here for 17/0 and 24/0 inner try-catch block is executing but for number 25 outer try-catch block is executing.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how outer
// try block will handle the exception
// which is not handled by the inner 
// try 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};
  
        // Outer try block
          
        // Here IndexOutOfRangeException occurs
        // due to which program may terminates
        try {
              
            for (int j = 0; j < number.Length; j++) {
  
                // Inner try block
                  
                // Here DivideByZeroException caught
                // and allow the program to continue 
                // its execution
  
                try {
                      
                    Console.WriteLine("Number: " + number[j] + 
                                      "\nDivisor: " + divisor[j] + 
                                      "\nQuotient: " + number[j] / divisor[j]);
                }
                  
                // Catch block for inner try block
                catch (DivideByZeroException) {
                      
                    Console.WriteLine("Inner Try Catch Block");
                }
            }
        }
  
        // Catch block for outer try block
        catch (IndexOutOfRangeException) {
              
            Console.WriteLine("Outer Try Catch Block");
              
        }
    }
}

chevron_right


Output:

Number: 8
Divisor: 2
Quotient: 4
Inner Try Catch Block
Inner Try Catch Block
Number: 5
Divisor: 5
Quotient: 1
Outer Try Catch Block

Example 2: In the below example, an exception is generated within the inner try block that is caught by the catch block associated with the inner try block.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// nested try block
using System;
  
public class Geeks {
  
    public string Author_name
    {
        get;
        set;
    }
}
  
// Driver Class
public class GFG {
      
      
    // Main Method
    public static void Main()
    {
        Geeks str1 = null;
  
        // outer try block
        try {
              
            // inner try block
            try {
                  
                str1.Author_name = "";
            }
  
            // catch block for the inner try block
            catch {
                  
                Console.WriteLine("Inner try catch block");
            }
        }
          
        // catch block for the outer try block
        catch {
              
            Console.WriteLine("Outer try catch block");
        }
    }
}

chevron_right


Output:

Inner try catch block


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.



Improved By : mayank5326



Article Tags :

Be the First to upvote.


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