Home

Exceptions in the .NET Framework

 

The Exception Class

In traditionally-oriented error dealing languages such as C/C++, Object Pascal, or Visual Basic, you could create any exception of your choice, including numeric or strings. To customize exception handling, you could also create your own class(es). Most libraries such as Borland's VCL and Microsoft's MFC also shipped with their own classes to handle exceptions. Even the Win32 library provides its type of mechanism to face errors. To support exception handling, the .NET Framework provides a special class called Exception. Once the compiler encounters an error, the Exception class allows you to identify the type of error and take an appropriate action.

Normally, Exception mostly serves as the general class of exceptions. Anticipating various types of problems that can occur in a program, Microsoft derived various classes from Exception to make this issue friendlier. As a result, almost any type of exception you may encounter already has a class created to deal with it. Therefore, when your program faces an exception, you can easily identify the type of error. There are so many exception classes that we cannot study or review them all. The solution we will use is to introduce or review a class when we meet its type of error.

The Exception's Message

In exception handling, errors are dealt with in the catch section. To do this, use catch as if it were a method. This means that, on the right side of catch, open a parenthesis, declare a variable of the type of exception you want to deal with. By default, an exception is first of type Exception. Based on this, a typical formula to implement exception handling is:

try
{
    // Process the normal flow of the program here
}
catch(Exception e)
{
    // Deal with the exception here
}

When an exception occurs in the try section, code compilation is transferred to the catch section. If you declare the exception as an Exception type, this class will identify the error. One of the properties of the Exception class is called Message. This property contains a string that describes the type of error that occurred. You can then access this Exception.Message property to display an error message if you want. Here is an example:

using System;

class Program
{
    static int Main()
    {
        double side;

        Console.WriteLine("Square Processing");
        try
        {
            Console.Write("Enter Side: ");
            side = double.Parse(Console.ReadLine());

            Console.WriteLine("\nSquare Characteristics");
            Console.WriteLine("Side:      {0}", side);
            Console.WriteLine("Perimeter: {0}", side * 4);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return 0;
    }
}

Here is an example of running the program:

Square Processing
Enter Side: Wer24
Input string was not in a correct format.
Press any key to continue . . .

Custom Error Messages

As you can see, one of the strengths of the Exception.Message property is that it gives you a good indication of the type of problem that occurred. Sometimes, the message provided by the Exception class may not appear explicit enough. In fact, you may not want to show it to the user since, as in this case, the user may not understand what the expression "correct format" in this context means and why it is being used. As an alternative, you can create your own message and display it to the user. Here is an example:

using System;

class Program
{
    static int Main()
    {
        double side;

        Console.WriteLine("Square Processing");
        try
        {
            Console.Write("Enter Side: ");
            side = double.Parse(Console.ReadLine());

            Console.WriteLine("\nSquare Characteristics");
            Console.WriteLine("Side:      {0}", side);
            Console.WriteLine("Perimeter: {0}", side * 4);
        }
        catch(Exception ex)
        {
            Console.WriteLine("The operation could not be carried because " +
                              "the number you typed is not valid");
        }

        return 0;
    }
}

Here is an example of running the program:

Square Processing
Enter Side: 24.Gh
The operation could not be carried because the number you typed is not valid
Press any key to continue . . .

You can also combine the Exception.Message message and your own message:

using System;

class Program
{
    static int Main()
    {
        double side;

        Console.WriteLine("Square Processing");
        try
        {
            Console.Write("Enter Side: ");
            side = double.Parse(Console.ReadLine());

            Console.WriteLine("\nSquare Characteristics");
            Console.WriteLine("Side:      {0}", side);
            Console.WriteLine("Perimeter: {0}", side * 4);
        }
        catch(Exception ex)
        {
            Console.Write(ex.Message);
            Console.WriteLine(" Consequently, The operation " +
			      "could not be carried because " +
                              "the number you typed is not valid");
        }

        return 0;
    }
}

Here is an example of running the program:

Square Processing
Enter Side: 25.KL48
Input string was not in a correct format.. Consequently, The operation could not
 be carried because the number you typed is not valid
Press any key to continue . . .
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next