By 22/07/2015 24 Comments

C# Nested Try And Catch Blocks

  • In C# one try block can contains another try block which is called nested try blocks.
  • Exception of an inner try block is handled by its inner catch block and exception of an outer try block is handled by its outer catch block.
  • If an exception is occurred in an outer try block then the flow control transfer to an outer catch block and inner exception or try block will be by passed.
  • If an exception does not occurred in an outer try block but occurred in an inner try block then that is handled by an inner catch block.
  • Remember the inner try blocks are associated with the inner catch blocks and outer are associated with the outer catch blocks.


using System;
namespace csharpAdvance
    class ExceptionExample
        // int type array.
        int[] Numbers = { 1, 2 };

        // void type method.
        public void Devision(int number, int devidedBy)
            Console.WriteLine("Division " + number + "/" + devidedBy + " = " + (number / devidedBy));

        // void type method GetArray().
        public void GetArray()
            // for loop loops through each int number and assigns into an array.
            for (int count = 0; count < 4; count++)
                Numbers[count] = count;

    class Program
        static void Main(string[] args)
            // Object creation.
            ExceptionExample exceptionExample = new ExceptionExample();

            { // An outer try block.

                { // An inner/nested try block.

                     exceptionExample.Devision(10, 0);

                } // End of an inner try block.

                catch (DivideByZeroException)
                { // An inner catch block is associated with an inner try block.

                     Console.WriteLine("DivideByZeroException is caught");

                } // End of an inner catch block.

            } // End of an outer try block.

            catch (IndexOutOfRangeException)
            { // An outer catch block is associated with an outer try block.

                 Console.WriteLine("IndexOutOfRangeException is caught");

            } // End of an outer catch block.
    /*  The Output will be:
        IndexOutOfRangeException is caught

Next Tutorial →

← Previous Tutorial

Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .