C# Tutorial Lesson 17: Exceptions

The exception handling in C#, and Java is quite similar. However, C# follows C++ in allowing the author to ignore more of the exceptions that might be thrown (an exception which is thrown but not caught will halt the program and may throw up a dialogue box).

To catch a particular type of exception in a piece of code, you have to first wrap it in a 'try' block and then specify a 'catch' block matching that type of exception. When an exception occurs in code within the 'try' block, the code execution moves to the end of the try box and looks for an appropriate exception handler. For instance, the following piece of code demonstrates catching an exception specifically generated by division by zero:

1.

try

2.

{

3.

    int zero = 0;

4.

    res = (num / zero);

5.

}

6.

catch (System.DivideByZeroException e)

7.

{

8.

    Console.WriteLine("Error: an attempt to divide by zero");

9.

}


You can specify multiple catch blocks (following each other), to catch different types of exception. A complication results, however, from the fact that exceptions form an object hierarchy, so a particular exception might match more than one catch box. What you have to do here is put catch boxes for the more specific exceptions before those for the more general exceptions. At most one catch box will be triggered by an exception, and this will be the first (and thus more specific) catch box reached.

Following the last 'catch' box you can also include a 'finally' box. This code is guaranteed to run whether or not an exception is generated. It is especially useful for cleanup code where this would be skipped in the 'try' box following an exception being thrown.

Where an exception is not caught by any of the subsequent 'catch' boxes, the exception is thrown upwards to the code which called the method in which the exception occurred (note that in C# the methods do not declare what exceptions they are throwing). This exception will keep on bubbling upwards until it is either caught by some exception handling in the code, or until it can go no further and causes the program to halt.

Note that the exceptions a program throws need not be limited to those automatically generated. A program can throw exceptions - including customised exceptions - whenever it wishes, using the 'throw' command. The code below gives examples of all the statements discussed above, with the 'getException' method showing how to throw an exception.

1.

using System;

2.

public class ExceptionDemo

3.

{

4.

    public static void Main ()

5.

    {

6.

        try

7.

        {

8.

            getException();

9.

        }

10.

        catch (Exception e)

11.

        {

12.

            Console.WriteLine("We got an exception");

13.

        }

14.

        finally

15.

        {

16.

            Console.WriteLine("The end of the program");

17.

        }

18.

    }

19.

20.

    public static void getException()

21.

    {

22.

        throw new Exception();

23.

    }

24.

}


Link Building Information