Some System Exceptions


The namespace System defines a number of standard exceptions. All derive from the class SystemException, and they are generated by the CLR runtime when errors occur.

ArrayTypeMismatchException The type of value being written is incompatible with the type of the array.
DivideByZeroException An attempt was made to divide by zero.
IndexOutOfRangeException An attempt was made to access outside the bounds of an array.
InvalidCastException A runtime cast attempt failed.
OutOfMemoryException An attempt to allocate memory failed.
OverflowException An arithmetic overflow occurred.
NullReferenceException An operation was attempted on a null reference.
StackOverflowException The system stack was overrun.

When an operation is attempted with a reference that is null, the exception NullReferenceException is thrown by the runtime. A null reference does not point to any object. The null keyword may be used to set a reference to null. The following program creates a null reference to an object then attempts to call a method.

// Exception13 - Null References

using System;

class X
{
    public void Show()
    { Console.WriteLine("In Show"); }

}

class Program
{
    static void Main()
    {
        try
        {
            X x = null;
            x.Show();
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("NullReferenceException encountered");
        }
    }
}

A NullReferenceException is thrown and caught in Main().