By 22/07/2015 10 Comments

C# Re-throwing Exceptions

  • The C# exceptions are always caught by the different types of catch handlers but some times that handled exceptions can be Re-throwing/re-generated manually.
  • When an exception is re-thrown then it can be caught by another or outer catch clause.
  • By re-thrown an exception the C# allows to multiple catch handlers to handle the same exception individually.
  • A re-thrown exception cannot be re-caught by a same catch handler.
  • An exception is re-thrown by using a throw keyword without specifying an expression.

Example:

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

        // void type method declaration.
        public void GenerateException(int number, int devidedBy)
        {
            try
            {
                Console.WriteLine("Division " + number + "/" + devidedBy + " = " + (number / devidedBy));

                // for loop loops through each int number and assigns into an array.
                for (int count = 0; count < 4; count++)
                {
                    Numbers[count] = count;
                    Console.WriteLine(Numbers[count]);
                }
            }

            // DivideByZeroException is handled by catch.
            catch (DivideByZeroException)
            {
                Console.WriteLine("An exception DivideByZeroException is caught.");
            }

            // IndexOutOfRangeException is handled by catch.
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("An exception IndexOutOfRangeException is caught & re-thrown in a class.");

                // The throw keyword is re-throwing this exception.
                throw;
            }
        }
    }

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

            try
            {
                // GenerateException() method is called inside try block.
                exceptionExample.GenerateException(10, 5);
            }

            // A re-thrown exception IndexOutOfRangeException is handled/caught in main method.
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("A re-thrown exception IndexOutOfRangeException is caught in a main method.");
            }

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Division 10/5 = 2
        0
        1
        An exception IndexOutOfRangeException is caught & re-thrown in a class.
        A re-thrown exception IndexOutOfRangeException is caught in a main method.
     */
}






← 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 .