Exception Hierarchy

In this section, we'll introduce the Exception class, Exception Hierarchy and how to design multiple catch blocks.

Exception class is the base class of all the exceptions in C#. In the following example, we'll output the properties of the exception ArrayTypeMismatchException in the catch block.

Example 01-86-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

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

    static void Main()
    {
        try
        {
            genException();
        }
        catch (Exception e)
        {
            Console.WriteLine("Message:{0}", e.Message);
            Console.WriteLine("Source:{0}", e.Source);
            Console.WriteLine("StackTrace:{0}", e.StackTrace);
            Console.WriteLine("HelpLink:{0}", e.HelpLink);
            Console.WriteLine("HResult:{0}", e.HResult);
            Console.WriteLine("TargetSite:{0}", e.TargetSite);
            Console.WriteLine("InnerException:{0}", e.InnerException == null ? "null" : "Not null");
        }

        Console.Read();
    }
}

Output

Message:Attempted to access an element as a type incompatible with the array.
Source:HelloWorld
StackTrace:   at Program.genException() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 7
   at Program.Main() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 14
HelpLink:
HResult:-2146233085
TargetSite:Void genException()
InnerException:null

Explanation

  • Line 5-8: Throw an ArrayTypeMismatchException.
  • Line 16: Catch the exception. The exception object actually belongs to ArrayTypeMismatchException.
  • Line 18: Output the exception or error message.
  • Line 19: Output the name of the application.
  • Line 20: Output the call stack of the exception.
  • For the other properties, you can check it here.

The following is an example of the Exception Hierarchy in C#. It is not including all the exception classes.

C# Exception Hierarchy

There are two classes SystemException class and ApplicationException class which inherit from Exception class directly. As you can see, we have much more exception classes derived from SystemException class which is thrown by CLR. Microsoft recommends using ApplicationException class in user-defined applications. We'll demonstrate it in throw statement section.

Since the exception hierarchy exists, it is good design to catch exceptions from a derived exception to its base one. So the last one should be Exception class. See the example blow.

Example 01-86-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;

class Program
{
    static void throwIndexOutofRangeException()
    {
        int[] a = new int[3];
        a[3] = 3;   // IndexOutOfRangeException is thrown
    }

    static void Main()
    {
        try
        {
            throwIndexOutofRangeException();
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("Catch a NullReferenceException.");
        }
        catch (SystemException e)
        {
            Console.WriteLine("Catch a SystemException.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Catch an Exception.");
        }
        
        Console.Read();
    }
}

Output

Catch a SystemException.

Explanation

  • Line 8: An IndexOutOfRangeException will be thrown here by CLR. There is no try-catch statement to catch the exception so the control goes to calling method Main().
  • Line 13-28: try-catch block with 3 catch statements. Either SystemException catch block or Exception catch block is put ahead of NullReferenceException catch block will get a compile-time error. Because it makes NullReferenceException catch block unreachable.
  • Line 17-20: catch NullReferenceException but this is an IndexOutOfRangeException so the control jumps to the next catch statement.
  • Line 21-24: The exception is caught here because IndexOutOfRangeException belongs to SystemException.
  • Line 30: After the exception is caught, the control goes here.