QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

ON ERROR is used with GOTO to handle errors in a program.

Syntax

ON ERROR GOTO {lineNumber lineLabel}

Description

Example(s)

Using an error handler that ignores any error.


 ON ERROR GOTO Errhandler
   ' Main module program error simulation code
 ERROR 7           ' simulate an Out of Memory Error
 PRINT "Error handled...ending program"
 SLEEP 4
 SYSTEM            ' end of program code

 Errhandler:              'error handler sub program line label
  PRINT "Error"; ERR; "on program file line"; _ERRORLINE
  BEEP             ' warning beep
 RESUME NEXT       ' moves program to code following the error. 


Error 7 on program file line 3 
Error handled...ending program

Explanation: The ON ERROR statement is normally placed at the beginning of the main module code. Errhandle is the line label sub referred to in the statement. The handler prints the error code and attempts to use the next line of code using RESUME NEXT which is only used in error handling procedures. _ERRORLINE returns the program file’s actual text line count found in the IDE.

Using an error handler in a SUB procedure.


s
END

hand:
PRINT "got error!"
RESUME NEXT

SUB s
ON ERROR GOTO hand
ERROR 1
ON ERROR GOTO 0
PRINT "Done!"
END SUB 

Explanation: The GOTO procedure must be in the main code area after END to avoid a RESUME error later. Use GOTO 0 to clear the ON ERROR set in the sub so that later errors are not handled by it.

See Also