QBasic 1.1: ON ERROR Statement
Explanation
ON ERROR Statement
Enables error handling and, when a run-time error occurs, directs your program to either branch to an error-handling routine or resume execution.
Worth knowing
Useful and cross-version information about the programming environments of QBasic, QuickBasic and Visual Basic for DOS.
Syntax
ON ERROR {GOTO line | RESUME NEXT} |
Description / Parameter(s)
GOTO line |
Branches to the first line of the error-handling routine, specified by a label or line number. To disable error handling, specify GOTO 0. |
RESUME NEXT |
Resumes execution with the statement following the statement that caused the run-time error. Use the ERR function to obtain the error code for the error. |
If ON ERROR isn't used, any run-time error ends your program. |
Example
'Illustrates ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME.
ON ERROR GOTO Handler
10 CHDIR "a:\" 'Causes ERR 71 "Disk not ready"
'if no disk in Drive A.
20 y% = 0
30 x% = 5 / y% 'ERR 11 "Division by zero."
40 PRINT "x% ="; x%
50 ERROR 57 'ERR 57 "Device I/O error."
Handler:
PRINT
PRINT "Error "; ERR; " on line "; ERL
SELECT CASE ERR
CASE 71
PRINT "Using device "; ERDEV$; " device error code = "; ERDEV
RESUME NEXT
CASE 11
INPUT "What value do you want to divide by"; y%
RESUME 'Retry line 30 with new value of y%.
CASE ELSE
PRINT "Unexpected error, ending program."
END
END SELECT