ON ERROR: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "ON ERROR is used with GOTO to handle errors in a program. {{PageSyntax}} : ON ERROR GOTO {''lineNumber''|''lineLabel''} {{PageDescription}} * An ''untreated error'' in a program will cause execution to stop and an error message is displayed to the user, who can choose to continue (ignore the error - which could have unexpected results) or end the program. * Use ON ERROR when your program performs operations that are likely to generate errors, like...") |
No edit summary |
||
Line 9: | Line 9: | ||
* An ''untreated error'' in a program will cause execution to stop and an error message is displayed to the user, who can choose to continue (ignore the error - which could have unexpected results) or end the program. | * An ''untreated error'' in a program will cause execution to stop and an error message is displayed to the user, who can choose to continue (ignore the error - which could have unexpected results) or end the program. | ||
* Use [[ON ERROR]] when your program performs operations that are likely to generate errors, like file access operations. | * Use [[ON ERROR]] when your program performs operations that are likely to generate errors, like file access operations. | ||
* [[ON ERROR]] statements can be in the main module code or in [[SUB]] or [[FUNCTION]] procedures. | * [[ON ERROR]] statements can be in the main module code or in [[SUB]] or [[FUNCTION]] procedures. | ||
* [[ON ERROR]] statements take precedence in the order they are encountered. It will also handle any subroutine errors. | * [[ON ERROR]] statements take precedence in the order they are encountered. It will also handle any subroutine errors. | ||
* '''ON ERROR GOTO 0''' can be used to disable custom [[ON ERROR]] trapping and give default error messages. | * '''ON ERROR GOTO 0''' can be used to disable custom [[ON ERROR]] trapping and give default error messages. | ||
* A subsequent ON ERROR statement will override the previous one. | * A subsequent ON ERROR statement will override the previous one. | ||
* [[GOTO]] is required in the statement. Cannot use [[GOSUB]]. | * [[GOTO]] is required in the statement. Cannot use [[GOSUB]]. | ||
Line 21: | Line 21: | ||
''Example 1:'' Using an error handler that ignores any error. | ''Example 1:'' Using an error handler that ignores any error. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|ON ERROR}} {{Cl|GOTO}} Errhandler | {{Cl|ON ERROR}} {{Cl|GOTO}} Errhandler | ||
' Main module program error simulation code | ' Main module program error simulation code | ||
Line 32: | Line 32: | ||
PRINT "Error"; {{Cl|ERR}}; "on program file line"; {{Cl|_ERRORLINE}} | PRINT "Error"; {{Cl|ERR}}; "on program file line"; {{Cl|_ERRORLINE}} | ||
{{Cl|BEEP}} ' warning beep | {{Cl|BEEP}} ' warning beep | ||
{{Cl|RESUME}} NEXT ' moves program to code following the error. | {{Cl|RESUME}} NEXT ' moves program to code following the error. | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Error 7 on program file line 3 | Error 7 on program file line 3 | ||
Error handled...ending program | Error handled...ending program | ||
{{OutputEnd}} | {{OutputEnd}} | ||
Line 42: | Line 42: | ||
''Example 2:'' Using an error handler in a [[SUB]] procedure. | ''Example 2:'' Using an error handler in a [[SUB]] procedure. | ||
{{CodeStart}} | {{CodeStart}} | ||
s | s | ||
{{Cl|END}} | {{Cl|END}} | ||
Line 55: | Line 55: | ||
{{Cl|ON ERROR}} {{Cl|GOTO}} 0 | {{Cl|ON ERROR}} {{Cl|GOTO}} 0 | ||
{{Cl|PRINT}} "Done!" | {{Cl|PRINT}} "Done!" | ||
{{Cl|END SUB}} | {{Cl|END SUB}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''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. | : ''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. |
Revision as of 02:12, 23 January 2023
ON ERROR is used with GOTO to handle errors in a program.
Syntax
Description
- An untreated error in a program will cause execution to stop and an error message is displayed to the user, who can choose to continue (ignore the error - which could have unexpected results) or end the program.
- Use ON ERROR when your program performs operations that are likely to generate errors, like file access operations.
- ON ERROR statements can be in the main module code or in SUB or FUNCTION procedures.
- ON ERROR statements take precedence in the order they are encountered. It will also handle any subroutine errors.
- ON ERROR GOTO 0 can be used to disable custom ON ERROR trapping and give default error messages.
- A subsequent ON ERROR statement will override the previous one.
- GOTO is required in the statement. Cannot use GOSUB.
- Comment out ON ERROR to find specific error locations. QB64 can return the file line position with _ERRORLINE
- Note: QB64 does not support the PDS (QuickBASIC 7) ON ERROR RESUME NEXT statement.
Examples
Example 1: 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.
Example 2: 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
- ERR, ERL, RESUME
- ON...GOTO
- _ERRORLINE, _INCLERRORLINE, _INCLERRORFILE$
- ERROR (simulates an error)
- ERROR Codes