|
|
|
|
|
|
|
the particular system you are using. When testing a program that performs floating point calculations, determine the acceptable margin of error beforehand, and then design your test data to try to push the program beyond those limits. Carefully check the accuracy of the computed results. (Remember that when you hand calculate the correct results, a pocket calculator may have less precision than your computer system.) If the program produces acceptable results when given worst-case data, it probably performs correctly on typical data. |
|
|
|
|
|
|
|
|
Several times in this book we had our programs test for invalid data and write an error message. Writing an error message is certainly necessary, but it is only the first step. We must also decide what the program should do next. The problem itself and the severity of the error should determine what action is taken in any error condition. The approach taken also depends on whether or not the program is being run interactively. |
|
|
|
|
|
|
|
|
In a program that reads its data only from an input file, there is no interaction with the person who entered the data. The program, therefore, should try to adjust for the bad data items, if at all possible. |
|
|
|
|
|
|
|
|
If the invalid data item is not essential, the program can skip it and continue; for example, if a program averaging test grades encounters a negative test score, it could simply skip the negative score. If an educated guess can be made about the probable value of the bad data, it can be set to that value before being processed. In either event a message should be written stating that an invalid data item was encountered and outlining the steps that were taken. Such messages form an exception report. |
|
|
|
|
|
|
|
|
If the data item is essential and no guess is possible, processing should be terminated. A message should be written to the user with as much information as possible about the invalid data item. |
|
|
|
|
|
|
|
|
In an interactive environment, the program can prompt the user to supply another value. The program should indicate to the user what is wrong with the original data. Another possibility is to write out a list of actions and ask the user to choose among them. |
|
|
|
|
|
|
|
|
These suggestions on how to handle bad data assume that the program recognizes when bad data have been entered. There are two approaches to error detection: passive and active. Passive error detection leaves it to the system to detect errors. This may seem easier, but the programmer relinquishes control of processing when an error occurs. An example of passive error detection is the system's division-by-zero error. |
|
|
|
|
|
|
|
|
Active error detection means having the program check for possible errors and determine an appropriate action if an error is encountered. An example of active error detection would be to read a value and use an If statement to see if the value is zero before dividing it into another number. |
|
|
|
|
|
|
|
|
The BirthdayReminder program uses no error detection. If the input is typed incorrectly, the program either produces no output at all (if the first |
|
|
|
|
|