Home

Custom Exceptions

 

Introduction

As seen in the previous lesson and in the above sections, exception handling is a great part of the .NET Framework. As high as it is supported by various classes of the .NET, it is possible that you want to further customize the handling of exceptions in your application. One way you can do this is to create your own exception class.

Creating an Exceptional Class

The Exception class of the .NET Framework is a great tool for handling exceptions in a C# application. To deal with particular errors, various classes are derived from Exception. If for some reason the Exception class and none of the Exception-based classes fulfills your requirement, you can derive a new class from Exception or from one of the available Exception-based classes.

To derive a class from Exception or from one of its classes, simply follow the rules we reviewed from class inheritance. Here is an example of a class based on Exception:

public class CustomException : Exception 
{
}

There is no real rule to follow as to what class to derive from but it may be a good idea to derive your class from one that already addresses your issue but not the way you want. For example, if you want to create a class that would process numeric values but you think the FormatException class is not doing what you want, you can derive your class from FormatException.

After deriving the class, you can add the necessary members as you see fit. Remember that the primary characteristic of an exception is to present a message to the user. In Exception-based classes, this message is represented by the Message property. Therefore, if you want to prepare a custom message for your class, you can override or new this property. Here is an example:

public class IntegerException : Exception 
{
    public override string Message
    {
        get
        {
            return "The value you entered is not a valid integer";
        }
    }
}

Once you have created and customized your exceptional class, you can use it the same way you would another exception class, such as throwing it. Here is an example

using System;

public class DigitException : Exception
{
    private char c;

    public DigitException(char x)
    {
        c = x;
    }

    public override string Message
    {
        get
        {
            return "The character you entered is not a valid digit";
        }
    }
}

class Program
{
    static int Main()
    {
        try
        {
            char chNumber = '0';

            Console.Write("Enter a digit (0 to 9): ");
            chNumber = char.Parse(Console.ReadLine());

            if ((chNumber != '0') &&
                (chNumber != '1') &&
                (chNumber != '2') &&
                (chNumber != '3') &&
                (chNumber != '4') &&
                (chNumber != '5') &&
                (chNumber != '6') &&
                (chNumber != '7') &&
                (chNumber != '8') &&
                (chNumber != '9'))
                 throw new DigitException(chNumber);
           
            Console.WriteLine("Number: {0}\n", chNumber);
        }
        catch (DigitException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (FormatException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return 0;
    }
}

Here is an example of running the program:

Enter a digit (0 to 9): 8
Number: 8

Press any key to continue . . .

Here is another example of running the program:

Enter a digit (0 to 9): w
The character you entered is not a valid digit
Press any key to continue . . .

 

 

Previous Copyright © 2006-2016, FunctionX, Inc.