Error Handling
Without error handling, a run-time error stops the program with a message such as "Division by zero" (QB64-PE shows a dialog offering to continue or quit).
18.1 Run-time errors
Without error handling, a run-time error stops the program with a message such as "Division by zero" (QB64-PE shows a dialog offering to continue or quit). Common error codes:
Bad file name or number Illegal function call File not found File already open Subscript out of range Division by zero Input past end of file Out of string space RESUME without error Path/File access error Path not found
18.2 ON ERROR GOTO and RESUME
ON ERROR GOTO label trap errors, jump to label ON ERROR GOTO 0 disable the trap (errors stop the program again) retry the statement that caused the error continue after the failing statement RESUME label continue at a label error code of the last error line number of the error (only meaningful with line numbers) source line of the error (QB64) raise error n yourself (useful for testing handlers) Rules in QBasic/QB64:
The handler label must be in the main module (not inside a SUB/FUNCTION). Errors inside procedures jump to that module-level handler, then RESUME NEXT returns into the procedure.
A handler must end with RESUME (or END/SYSTEM); falling off the end of the handler code is a bug.
An error inside the handler itself is not trapped.
ON ERROR GOTO Handler ' enable the error trap
INPUT "Divide 100 by: ", d
result = 100 / d
PRINT "Result:"; result
OPEN "missing.txt" FOR INPUT AS #1
PRINT "This line runs after the handler skips the failed OPEN."
END
Handler:
PRINT "Error"; ERR; "on line"; _ERRORLINE
SELECT CASE ERR
CASE 11
PRINT "Cannot divide by zero; using 1."
d = 1
RESUME ' retry the statement that failed
CASE 53
PRINT "File not found."
RESUME NEXT ' continue with the statement after the failed one
CASE ELSE
PRINT "Unexpected error. Stopping."
END
END SELECT18.3 Defensive programming: prevent instead of trap
Error trapping is a safety net; most errors are better prevented:
Classic BASIC: GW-BASIC had ON ERROR GOTO, RESUME, ERR, ERL; many 8-bit BASICs had no error trapping at all (BBC BASIC used ON ERROR with ERR and ERL; Commodore BASIC V2 had none). FreeBASIC: On Error Goto works in -lang qb/fblite; in -lang fb the usual approach is checking return codes (Open returns an error code as a function: If Open("f.txt" For Input As #1) <> 0 Then ...) and compiling with -exx for runtime checks. VB.NET: structured exceptions: Try ... Catch ex As FileNotFoundException ... Catch ex As Exception ... Finally ... End Try, Throw New ArgumentException("..."), Using blocks for automatic cleanup. Legacy On Error still compiles but is discouraged.
IF d <> 0 THEN result = 100 / d ELSE PRINT "Cannot divide by zero"
IF _FILEEXISTS("config.ini") THEN
OPEN "config.ini" FOR INPUT AS #1
' ...
CLOSE #1
ELSE
PRINT "Using default settings."
END IF
IF idx >= LBOUND(a) AND idx <= UBOUND(a) THEN PRINT a(idx)
INPUT "Age: ", age$
IF VAL(age$) <= 0 OR VAL(age$) > 150 THEN PRINT "Invalid age"