Home > Articles > Microsoft > Other Microsoft

  • Print
  • + Share This
This chapter is from the book

Understanding Exceptions

An exception occurs when a program encounters any unexpected problems such as running out of memory or attempting to read from a file that no longer exists. These problems are not necessarily caused by programming errors but mainly occur due to violations of certain assumptions that are made about the execution environment.

When a program encounters an exception, its default behavior is to throw the exception, which generally translates to abruptly terminating the program after displaying an error message. This is not a characteristic of a robust application and does not make your program popular with users. Your program should be able to handle these exceptional situations and, if possible, gracefully recover from them. This is called exception handling. Proper use of exception handling can make programs robust and easy to develop and maintain. However, if you do not use exception handling properly, you might end up having a program that performs poorly, is harder to maintain, and may potentially mislead its users.

Step by Step 3.1 demonstrates how an exception may occur in a program. Later in this chapter I explain how to handle these exceptions.

STEP BY STEP 3.1 - Exceptions in Windows Applications

  1. Create a new C# Windows application project in the Visual Studio .NET Integrated Development Environment (IDE). Name the project 316C03.

  2. Add a new Windows form to the project. Name it StepByStep3_1.

  3. Place three TextBox controls (txtMiles, txtGallons, and txtEfficiency) and a Button control (btnCalculate) on the form and arrange them as shown in Figure 3.1. Add Label controls as necessary.

Figure 3.1Figure 3.1 The mileage efficiency calculator does not implement any error handling for the user interface.

  1. Add the following code to the Click event handler of btnCalculate:

    private void btnCalculate_Click(
      object sender, System.EventArgs e)
      //this code has no error checking. If something
      //goes wrong at run time,
      //it will throw an exception
      decimal decMiles =
      decimal decGallons =
      decimal decEfficiency = decMiles/decGallons;
      txtEfficiency.Text =
        String.Format("{0:n}", decEfficiency);


  2. Insert the Main() method to launch the form. Set the form as the startup object for the project.

  3. Run the project. Enter values for miles and gallons and click the Calculate button. The program calculates the mileage efficiency, as expected. Now enter the value 0 in the Gallons of Gas Used field and run the program. The program abruptly terminates after displaying an error message (see Figure 3.2).

Figure 3.2Figure 3.2 The development environment gives you a chance to analyze the problem when an exception occurs.

When you run the program created in Step by Step 3.1 from the IDE and the program throws an exception, the IDE gives you options to analyze the problem by debugging the program. In Step by Step 3.1, if you had instead run the program by launching the project's .exe file from Windows Explorer, the program would have terminated after displaying a message box with an error message and some debugging information (see Figure 3.3).

Figure 3.3Figure 3.3 When a program is executed outside the Visual Studio .NET environment, debugging information is displayed when an exception is thrown.

From the CLR's point of view, an exception is an object that encapsulates information about the problems that occur during program execution. The FCL provides two categories of exceptions:

  • ApplicationException—Represents exceptions thrown by the applications

  • SystemException—Represents exceptions thrown by the CLR

Both of these exception classes derive from the base class Exception, which implements the common functionality for exception handling. Neither the ApplicationException class nor the SystemException class adds any new functionality to the Exception class; they exist just to differentiate exceptions in applications from exceptions in the CLR. The classes derived from Exception share some important properties, as listed in Table 3.1

Table 3.1 Important Members of the Exception Class






Specifies the uniform resource locator (URL) of the help file associated with this exception.



Specifies an exception associated with this exception. This property is helpful when a series of exceptions are involved. Each new exception can preserve the information about the previous exception by storing it in the InnerException property.



Specifies textual information that indicates the reason for the error and provides possible resolutions.



Specifies the name of the application that causes the error.



Specifies where an error has occurred. If the debugging information is available, the stack trace includes the name of the source file and the program line number.



Represents the method that throws the current exception.

  • + Share This
  • 🔖 Save To Your Account