Error
Handling
4-2
Underflow. Single-precision floating-point underflow occurs when the magnitude
of
a single-precision numeric value
is
less than
(±)
1.2 x
10-
38
(1.2E-38). Double-
precision floating-point underflow occurs when the magnitude of a double-
precision numeric value
is
less
than
(±)
2.2 x
10-
308
(2.2D-308). When a value of this
magnitude
is
generated, no message
is
printed, the result
is
defined
as
zero, and pro-
gram execution continues.
Divide-By-Zero. Divide-by-zero
is
handled identically to single- and double-
precision overflow protocol, except that the message printed
is
DIVIDE-BY -ZERO.
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
-32768.49
is
converted to integer representation. Integer underflow cannot occur.
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 error-service routine at the
specified location to identify and correct errors. In the following example, when
-3
is
entered in response to the INPUT statement, an error occurs and the user-written
error message displays:
10
ON ERROR GOTO
100
20
INPUT A
30
PRINT SQR{A)
40
GOTO
20
100
PRINT
"ARGUMENT
CANNOT
BE
NEGATIVE."
110
PRINT
"ENTER
NEW
VALUE."
120
RESUME NEXT
RUN

? 3

1.732051

?

-3

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 error-service
routine at line 100, which identifies one specific error (division by zero) and the line
number in which it occurs:
BASIC-SO