C# Exception

In C#, an exception can be generated whenever an application encounters any exceptional or unexpected situations, for example, a division by zero. When an exception occurs, we'll use try-catch block to catch the exception and handle it by an exception handler. If there is no exception handler defined, the program will stop running with an error message. In most cases, exceptions are generated by CLR but sometimes we can generate exceptions explicitly using the throw keyword.

The following are the examples of several exceptions generated by CLR and a comment is added at the end of the statement which will issue an exception.

DivideByZeroException is thrown when trying to divide a integral value by zero.

Example 01-84-01

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program
{
    static void Main()
    {
        int i = 10;
        int j = 0;

        Console.WriteLine("i / j = ", i / j);   // DivideByZeroException was issued.
        Console.Read();
    }
}

ArrayTypeMismatchException is thrown when an element whose type is incompatible with the actual type of an array is trying to be saved in the array.

Example 01-84-02

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program
{
    static void Main()
    {
        object[] o = (object[]) new string[]{"a", "b", "c"};
        o[1] = 1;   // ArrayTypeMismatchException is issued.

        Console.WriteLine("o[1] = ", o[1]);
        Console.Read();
    }
}

Explanation

  • Line 7: Create an object array and the actual type of the array is string.
  • Line 8: ArrayTypeMismatchException is issued because an integer cannot be saved in a string array.

IndexOutOfRangeException is thrown when an element whose index is out of the range of the array is accessed.

Example 01-84-03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System;

class Program
{
    static void Main()
    {
        int[] i = new int[6];
        i[6] = 1;   // IndexOutOfRangeException is issued.

        Console.WriteLine("i[6] = ", i[6]);
        Console.Read();
    }
}

Explanation

  • Line 7: Create an integer array with 6 elements. The indexes of the elements start with 0 and end with 5.
  • Line 8: IndexOutOfRangeException is issued because i[6] is illegal and the last element of the array should be i[5].

InvalidCastException is thrown when a casting from a base type to a derived type fails at runtime.

Example 01-84-04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

interface I{ }
public class A : I { }
public class B : I { }

class Program
{
    static void Main()
    {
        I i = new A();
        B b = (B)i; // InvalidCastException is issued

        Console.WriteLine("b = ", b);
        Console.Read();
    }
}

Explanation

  • Line 3-5: Define an interface and 2 derived classes implementing the interface.
  • Line 11: The actual object is A.
  • Line 12: InvalidCastException is issued because the object of class A cannot be assigned to the variable of class B.

NullReferenceException is thrown whenever you reference a null object.

Example 01-84-05

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;

class Program
{
    public int i;

    static void Main()
    {
        Program p = null;
        p.i = 10;   // NullReferenceException is issued

        Console.WriteLine("p.i = ", p.i);
        Console.Read();
    }
}