Error Handling |
Underflow.
Integer Operations
Integer overflow and division by zero are treated as normal errors. Program execu- tion halts. Integer overflow occurs when a value greater than 32767.49 or less than
Error Trapping
Error trapping allows you to trap and correct errors. Errors normally halt program execution and return error messages. When given before an error is encountered, the ON ERROR GOTO statement transfers program execution to a specified line number when an error occurs, and suppresses any error messages other than those you specify and write yourself. You can write an
10ON ERROR GOTO 100
20INPUT A
30PRINT SQR{A)
40GOTO 20
100PRINT "ARGUMENT CANNOT BE NEGATIVE."
110PRINT "ENTER NEW VALUE."
120RESUME NEXT RUN
? 3
1.732051
?
ARGUMENT CANNOT BE NEGATIVE ENTER NEW VALUE
?
Suppose there is no error routine (made up of lines 10, 100, 110, and 120) in the above example, and you wish to identify this particular error. The ERR and ERL functions return the error code number (see Appendix A) and the line number the error occurs in, respectively. In this way, you can identify the kind of error and the line it occurs in and write individual error handling routines for individual errors. The program below uses ERL and ERR to identify the location and nature of any error.
In the following example, the ON ERROR statement enables the