By 22/07/2015 6 Comments

Creating Custom Exceptions In C#

  • The C# provides several built-in exception types which are system level exceptions. These exceptions are generated/thrown by C# run-time system and also handled automatically by C.L.R (common language run-time compiler).
  • Like C#’s system level built-in exceptions we can also generate/throw our own custom exceptions in our programs and handle/catch them by our own custom classes.
  • It is easy to creating the custom exception classes, for do that we just inherit our custom derived class from a base System.Exception class. Our custom derived exception class contains all members (methods, properties etc.) of base System.Exception class and our derived class don’t need to implement any member of a base System.Exception class. However our custom exception class can override all virtual members of a base System.Exception class.
  • Remember our custom exception classes must call/access the overloaded constructors in a base Exception class through a base keyword.
  • Microsoft also recommends to derive custom exception classes from a base Exception class.


Example, A custom exception class:

using System;
namespace csharpAdvance
{
    // A custom user-defined exception class inherits base System.Exception class.
    class CustomZeroException : Exception
    {
        // Four constructors.
        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;
        }
    }

    // Create a Calculator class.
    class Calculator
    {
        // Static void method.
        public static void GetDivision(int number, int dividedBy)
        {
            // if statement checks the condition.
            if (dividedBy != 0)
            {
                int result = number / dividedBy;
                Console.WriteLine("Division answer: {0}", result);
            }
           
            // Inside else statement a custom exception is being thrown with a custom error message.
            else
                throw new CustomZeroException("Any value cannot divided by 0 in mathematics !");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // GetDivision() method inside try block.
                Calculator.GetDivision(10, 0);
            }

            // CustomZeroException is handel/caught by catch.
            catch (CustomZeroException customZeroException)
            {
                // Error message.
                Console.WriteLine(customZeroException.Message);
            }
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Any value cannot divided by 0 in mathematics !
     */
}






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