16K cassette or disk
by Donald B. Wilcox
It is often frustrating to be forced to restart a software program because an inadvertent error caused the program to crash. ATARI BASIC provides a special word -- TRAP -- that often can be used to prevent a program from ending before intended. Many errors are subject to automatic correction or compensation through a little extra effort on the part of the programmer.
If you are not yet familiar with the TRAP statement, the following example shows how to use it to detect INPUT errors. These occur when the user of a program types invalid numeric values into a numeric variable.
10 INPUT X
20 PRINT X
30 GOTO 10
In the above listing, typing a non-numeric response to the INPUT statement in line 10 (such as accidentally pressing return with no number entered) will result in an "ERROR-8 AT LINE 10" message. By adding a TRAP statement, this problem will be avoided completely.
10 TRAP 10: INPUT X
20 PRINT X
30 GOTO 10
In the slightly modified example above, if an input error occurs, the TRAP statement will catch the error and go back to line 10 to try the INPUT again.
This short article presents some common errors that can be prevented with a few extra lines of code. After perusal of these five examples, you should be able to understand how to m ake your programs less vulnerable to errors that prematurely end your program.
PROGRAM ONE - If you mistakenly create a new file using a file name that already exists, you will destroy the already existing file. No error message will warn you of the impending disaster. Program ONE will prevent this.
PROGRAM TWO - If you try to OPEN a nonexistent file, you will get error message - 170 and your program will crash. This can be prevented by using Program TWO.
PROGRAM THREE - If you try to input data from a disk file beyond the end-of-file, you will get error message - 136 and your program will terminate. You may not always know beforehand where the file data ends, so an automatic end-of-file trap can be programmed easily to prevent error 136. Program THREE will solve this problem.
PROGRAM FOUR - You forgot to turn on your printer or interface unit and get error message 138. If you attempt to use the CONTINUE command after you turn on the correct unit, your program will continue beginning at the line number that follows the line that caused the error. Often this can create erroneous results (not always detected) because the instructions on the line that caused the error may not have been executed correctly before the error.
PROGRAM FIVE - You are reading in data with a READ statement and you do not want to use an end-of-data dummy value as a flag nor do you want to count the entries to determine when all the data has been read. Program FIVE demonstrates a simple method to prevent error #6 (out of data) from prematurely terminating your program.
Finally, for those of you who are relatively new to ATARI BASIC, there are several locations (addresses) that you may PEEK to find out which error occured and which line caused the error. Location 195 contains the error number. Locations 186 and 187 contain the line number where the error occured, low byte, high byte respectively. To display this information on your screen, use the following statements as one method.
10 REM DISPLAY ERROR NUMBER
20 REM AND LINE NUMBER OF ERROR
30 ? PEEK(195);" AT LINE ";PEEK(186)+PEEK(187)*256
Happy trapping in your future programs.