Chapter 7: Exception Handling


Download CD Content

In any programming situation various problems can occur. You can have basic errors of many types. Handling those problems is of paramount importance. You do not want your program to crash when an error occurs. If this were to occur, the people using your software (programmers generally refer to them as “users”) would be very unsatisfied with your product. A better idea is to correct the problem or at least give the user a friendly message telling them what has occurred. Exception handling is, therefore, a critical topic in programming.

Different programmers have different philosophies regarding exception handling. However, it is commonly believed that you cannot have too much exception handling. Code should be contained inside some exception handling.

Errors and Exceptions

There is an almost limitless list of things that can go wrong. Think back to Chapter 5: What would happen if you tried to open a file that wasn’t there? You would get an error, and without error handling your program would crash. What if you asked a user to enter a number and they typed in their name, then your code tried to store their input in an integer variable? You would get an error, and, yes, without error handling your program would crash.

Errors can be grouped into three main categories, each of which is described in Table 7.1.

Table 7.1: Categories of Errors

Error Type

Description

Syntax

A syntax error occurs when you have attempted to use the language in a way that is inappropriate. For example if you said “if (x $ $ y)” when you meant to write “if (x==y)” that would be a syntax error. The compiler will not compile your code if you have any syntax errors.

Runtime

This is an error that occurs when you run your program. It is any interruption to the normal flow of your application. Trying to open a file that does not exist and dividing by 0 are two examples of runtime errors. These errors are also called exceptions.

Logic

This occurs when your program compiles and runs properly, but produces erroneous results. There is some error in your programming logic. This can be the hardest type of error to find and is what is generally referred to as a “bug.”

Now for the really bad news—you will have errors. It’s a fact of life that when you write programs you get errors. The first thing you can do is thoroughly test your programs to attempt to uncover and fix errors before distributing the program. The other thing you can do is provide adequate error handling so that errors don’t cause your program to crash. Error handling will take care of runtime errors and the compiler will identify syntax errors. Logic errors are what will be your biggest problem. At the end of this chapter, we will explore some techniques to help you in debugging applications.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net