By 22/07/2015 10 Comments

C# Catch Multiple Class Custom Exceptions

  • Through our own exception classes, base and derived or multiple class’s exceptions can be handled in our single class.
  • C# provides a rule for handle the multiple custom exceptions generated by base and derived classes, that rule is: A most inner exception derived class must be put first in the catch block sequences and a base class “Exception” put at last in catch clauses sequences.
  • If we do not follow that rule the compile time error will occur.
  • In general terms our custom exception classes contains all the four constructors which is defined in our base “Exception” class.

Example:

using System;
namespace csharpAdvance
{
    // A custom user-defined CustomZeroException class inherits base System.Exception class.
    class CustomZeroException : Exception
    {
        public CustomZeroException() : base() { }
        public CustomZeroException(string message) : base(message) { }
        public CustomZeroException(string message, Exception innerException) : base(message, innerException) { }

        // Override base class toString () method which returns an error message.
        public override string ToString()
        {
            return this.Message;
        }
    }

    // CustomOneException class inherits CustomZeroException class.
    class CustomOneException : CustomZeroException
    {
        public CustomOneException() : base() { }
        public CustomOneException(string message) : base(message) { }
        public CustomOneException(string message, Exception innerException) : base(message, innerException) { }

        public override string ToString()
        {
            return this.Message;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // for loop loops through 3 times.
            for (int count = 0; count <= 2; count++)
            {
                try
                {
                    // if statements compare a digit with loop’s count variable.
                    if (count == 0)
                        throw new CustomZeroException("An exception CustomZeroException has caught !");

                    if (count == 1)
                        throw new CustomOneException("An exception CustomOneException has caught !");

                    if (count == 2)
                        throw new Exception("A base class Exception has caught !");
                }

                // All exception is bieng caught by the following catch blocks.
                catch (CustomOneException exOne)
                {
                    Console.WriteLine(exOne.Message);
                }
                catch (CustomZeroException exZero)
                {
                    Console.WriteLine(exZero.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        An exception CustomZeroException has caught !
        An exception CustomOneException has caught !
        A base class Exception has 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 .