By 22/07/2015 11 Comments

C# Exception Classes

  • C# defines several built-in exceptions, actually these exceptions are classes which inherits System.SystemException class (directly or indirectly). The System.SystemException class inherits an System.Exception class which is a base class for all built-in exception classes or in other words all built-in exceptions are derived from an System.Exception class.
  • When run-time errors occur the CLR (common language run-time compiler) generates/throws these built-in exceptions automatically.
  • Since the System.Exception class is a base or father class of all built-in exceptions. It defines several members properties, methods and constructors. It contains read-only virtual properties such as Message, StackTrace and TargetSite, read-write properties such as HelpLink and Source. These different properties display different error messages of exceptions. Notice that, these are virtual properties it means that can be overridden in our own classes which inherits the base exception class.
  • Along with above properties, an Exception class also defines methods such as toString () and four overloaded constructors.
  • The custom exception classes can also be created by inheriting a base System.Exception class to them.

Following are some built-in exception classes:

IndexOutOfRangeException This exception will be generated if we trying to store values to an array that are greater then array’s actual size.
NullReferenceException This exception will be generated if we access a null reference variable which does not refer to any object.
DivideByZeroException This exception will be generated if any number will be divided by 0.
OverflowException An arithmetic overflow occurred.

Example, using members of System.Exception class:

using System;
namespace csharpAdvance
    class Calculator
        // static void method with 2 parameters.
        public static void Division(int number, int dividedBy)
            Console.WriteLine("Exception standard error messages:");
            Console.WriteLine("Division " + number + "/" + dividedBy + " = " + (number / dividedBy));
            Console.WriteLine("This line does not display.");

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

            // DivideByZeroException is handled by catch.
            catch (DivideByZeroException ex)
             /* Following statements are displaying different information 
                of current exception by calling the properties of 
                System.Exception class.*/
                Console.WriteLine("Message: {0}", ex.Message+"\n");
                Console.WriteLine("StackTrace: {0}", ex.StackTrace + "\n");
                Console.WriteLine("TargetSite:{0}", ex.TargetSite + "\n");
                Console.WriteLine("HelpLink: {0}", ex.HelpLink = "ERROR!" + "\n");
                Console.WriteLine("Source: {0}", ex.Source = "Exception caught." + "\n");

                // Display an exception object information.
                Console.WriteLine("toString(): {0}", ex.ToString());

    /*  The Output will be:
        Exception standard error messages:

        Message: Attempted to divide by zero.

        StackTrace:    at csharpAdvance.Calculator.Division(Int32 number, Int32 dividedB
        y) in C:\Users\WIN8\Desktop\c-sharp-course\c-sharp-course\Program.cs:line 11
           at csharpAdvance.Program.Main(String[] args) in C:\Users\WIN8\Desktop\c-sharp
        -course\c-sharp-course\Program.cs:line 22

        TargetSite:Void Division(Int32, Int32)

        HelpLink: ERROR!

        Source: Exception caught.

        toString(): System.DivideByZeroException: Attempted to divide by zero.
           at csharpAdvance.Calculator.Division(Int32 number, Int32 dividedBy) in C:\Use
        rs\WIN8\Desktop\c-sharp-course\c-sharp-course\Program.cs:line 11
           at csharpAdvance.Program.Main(String[] args) in C:\Users\WIN8\Desktop\c-sharp
        -course\c-sharp-course\Program.cs:line 22


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