ON ERROR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(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
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[ON ERROR]] is used with [[GOTO]] to handle errors in a program.
The '''ON ERROR''' statement is used in conjunction with [[GOTO]] to handle errors in a program.




{{PageSyntax}}
{{PageSyntax}}
: [[ON ERROR]] [[GOTO]] {''lineNumber''|''lineLabel''}
; Legacy QuickBASIC/QBasic
: [[ON ERROR]] [[GOTO]] {''lineNumberOrLabel'' '''|''' ''0''}
; QB64-PE extension
: [[ON ERROR]] [[GOTO]] {'''_LASTHANDLER | _NEWHANDLER''' ''lineNumberOrLabel''}
 
 
{{PageParameters}}
* {{Parameter|lineNumberOrLabel}} must be a line number or program label in the main part of your program usually placed after the [[END]] or [[SYSTEM]] statement. Line numbers or labels inside of [[SUB]] or [[FUNCTION]] blocks are not allowed.
* {{Parameter|0}} (zero) will disable all program based error handling, i.e. errors remain ''unhandled''.




{{PageDescription}}
{{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.
* An ''unhandled error'' in a program will cause execution to stop and an error message box 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 code section or in [[SUB]] or [[FUNCTION]] procedures, but the line number or label must always be in the main code section.
* [[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, i.e. the most recently set handler is used. Subsequent '''ON ERROR''' statements will override the previous one.
* '''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 program based error trapping and give default error message boxes.
* 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]].
* Comment out [[ON ERROR]] to find specific error locations. QB64 can return the file line position with [[_ERRORLINE]]
* QB64 and QB64-PE do not support the PDS (QuickBASIC 7) {{InlineCode}}{{Cl|ON ERROR}} {{Cl|RESUME}} {{Cl|NEXT}}{{InlineCodeEnd}} statement.
* Note: QB64 does not support the PDS (QuickBASIC 7) '''ON ERROR RESUME NEXT''' statement.
----
* Since QB64-PE v3.14.1 the '''_NEWHANDLER''' and '''_LASTHANDLER''' keywords can be used to build up a history chain of the used error handlers. This is especially useful inside [[SUB]] or [[FUNCTION]] code provided in external libraries, when the handlers are unknown (see Example 3).
** Works also over several nested or recursive sub-/function calls, but care must be taken for correct exit conditions to avoid crashes due to extensive memory usage. Each use of '''_NEWHANDLER''' should have a matching use of '''_LASTHANDLER'''.
** Organize your code so that '''_NEWHANDLER''' is not executed 1000s of times in a tight loop, best use is at the entry point of a [[SUB]] or [[FUNCTION]]. Place '''_LASTHANDLER''' at the end point, but make sure it's also called when using [[EXIT SUB]] or [[EXIT FUNCTION]].
** Memory crashes {{Text|may only happen on unbalanced use|red}} of '''_NEW-/_LASTHANDLER''', if you have much more (several 10000s) '''_NEWHANDLER''' than '''_LASTHANDLER''' executions, so if you take the care and follow given recommendations, then everything will just work fine.
** The use of '''_LASTHANDLER''' is always save. If the history chain has no more enries, then it is synonymous to the ''zero(0)'' parameter of the legacy syntax, i.e. disabling program based error trapping.
** The use of the legacy {{InlineCode}}{{Cl|ON ERROR}} {{Cl|GOTO}} {{Text|0|#F580B1}}{{InlineCodeEnd}} will also completly reset/clear the handler history chain.
 
 
{{PageAvailability}}
<!-- QB64 = a version or none, QBPE = a version or all, Platforms = yes or no -->
<gallery widths="48px" heights="48px" mode="nolines">
File:Qb64.png|'''v0.610'''
File:Qbpe.png|'''all'''
File:Apix.png
File:Win.png|'''yes'''
File:Lnx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
<!-- Additional availability notes go below the gallery, e.g. -->
* The ''_NEWHANDLER'' and ''_LASTHANDLER'' functionality is introduced in '''QB64-PE v3.14.1'''.




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Using an error handler that ignores any error.
;Example 1: Using an error handler that prints the error and then continues with the next program line.
{{CodeStart}}
{{Text|<nowiki>'install our own error handler</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} errHandler
{{Text|<nowiki>'now simulate an "Out of Memory" error</nowiki>|#919191}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Text|<nowiki>'our error handler will return to this point</nowiki>|#919191}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Text|<nowiki>'now we disable our error handler</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} {{Text|0|#F580B1}}
{{Cl|COLOR}} {{Text|9|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Handler disabled."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Text|<nowiki>'without our own error handler the following error is reported</nowiki>|#919191}}
{{Text|<nowiki>'in the usual message box popup again</nowiki>|#919191}}
{{Cl|PRINT}} {{Text|<nowiki>"The next error will no longer be handled, press any key ..."</nowiki>|#FFB100}}: {{Cl|SLEEP}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|END}}
 
{{Text|<nowiki>'the error handler label must be part of the main program, local</nowiki>|#919191}}
{{Text|<nowiki>'labels inside a SUB/FUNCTION are not allowed for error handlers</nowiki>|#919191}}
errHandler:
{{Cl|COLOR}} {{Text|10|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Start handling error"</nowiki>|#FFB100}}; {{Cl|ERR}}; {{Text|<nowiki>"on program file line"</nowiki>|#FFB100}}; {{Cl|_ERRORLINE}};
{{Cl|BEEP}}: {{Cl|PRINT}} {{Text|<nowiki>"... done."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|RESUME}} {{Cl|NEXT}} {{Text|<nowiki>'returns execution to the code following the error</nowiki>|#919191}}
{{CodeEnd}}
{{OutputStart}}
{{Ot|Start handling error 7 on program file line 6 ... done.|#55ff55}}
Error was handled.
 
{{Ot|Handler disabled.|#5555ff}}
The next error will no longer be handled, press any key ...
{{OutputEnd}}
 
----
 
;Example 2: Using an alternative error handler in a [[SUB]] procedure.
{{CodeStart}}
{{Cl|ON ERROR}} {{Cl|GOTO}} mainHandler
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Text|LegacyErrorTest|#55FF55}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Cl|ON ERROR}} {{Cl|GOTO}} {{Text|0|#F580B1}}
{{Cl|COLOR}} {{Text|9|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Handler disabled."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"The next error will no longer be handled, press any key ..."</nowiki>|#FFB100}}: {{Cl|SLEEP}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|END}}


{{CodeStart}} '' ''
{{Text|<nowiki>'the error handler label must be part of the main program, local</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} Errhandler
{{Text|<nowiki>'labels inside a SUB/FUNCTION are not allowed for error handlers</nowiki>|#919191}}
  ' Main module program error simulation code
mainHandler:
{{Cl|ERROR}} 7           ' simulate an Out of Memory Error
{{Cl|COLOR}} {{Text|10|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Main handler starts handling error"</nowiki>|#FFB100}}; {{Cl|ERR}}; {{Text|<nowiki>"on program file line"</nowiki>|#FFB100}}; {{Cl|_ERRORLINE}};
PRINT "Error handled...ending program"
{{Cl|BEEP}}: {{Cl|PRINT}} {{Text|<nowiki>"... done."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|SLEEP}} 4
{{Cl|RESUME}} {{Cl|NEXT}}
{{Cl|SYSTEM}}           ' end of program code
{{Text|<nowiki>'an alternative error handler which we set from within a SUB/FUNCTION</nowiki>|#919191}}
{{Text|<nowiki>'however, the error handler label must still be part of the main program</nowiki>|#919191}}
subHandler:
{{Cl|COLOR}} {{Text|12|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Sub handler starts handling error"</nowiki>|#FFB100}}; {{Cl|ERR}}; {{Text|<nowiki>"on program file line"</nowiki>|#FFB100}}; {{Cl|_ERRORLINE}};
{{Cl|BEEP}}: {{Cl|PRINT}} {{Text|<nowiki>"... done."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|RESUME}} {{Cl|NEXT}}


Errhandler:              'error handler sub program line label
{{Cl|SUB}} {{Text|LegacyErrorTest|#55FF55}}
  PRINT "Error"; {{Cl|ERR}}; "on program file line"; {{Cl|_ERRORLINE}}
{{Text|<nowiki>'the next line overrides the currently active "mainHandler"</nowiki>|#919191}}
  {{Cl|BEEP}}             ' warning beep
{{Text|<nowiki>'with our new "subHandler"</nowiki>|#919191}}
{{Cl|RESUME}} NEXT      ' moves program to code following the error. '' ''
{{Cl|ON ERROR}} {{Cl|GOTO}} subHandler
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Text|<nowiki>'unfortunately the legacy QuickBASIC/QBasic syntax has no function to</nowiki>|#919191}}
{{Text|<nowiki>'restore back to the old handler, so you need to know which handler was</nowiki>|#919191}}
{{Text|<nowiki>'active and restore that manually</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} mainHandler
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Error 7 on program file line 3
{{Ot|Main handler starts handling error 7 on program file line 4 ... done.|#55ff55}}
Error handled...ending program
Error was handled.
 
{{Ot|Sub handler starts handling error 7 on program file line 33 ... done.|#ff5555}}
Error was handled.
 
{{Ot|Main handler starts handling error 7 on program file line 8 ... done.|#55ff55}}
Error was handled.
 
{{Ot|Handler disabled.|#5555ff}}
The next error will no longer be handled, press any key ...
{{OutputEnd}}
{{OutputEnd}}
:''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.
;Example 3: Same as example 2, but using QB64-PE specific extensions '''_NEWHANDLER''' and '''_LASTHANDLER'''.
{{CodeStart}} '' ''
{{CodeStart}}
s
{{Cl|ON ERROR}} {{Cl|GOTO}} mainHandler
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Text|QB64peErrorTest|#55FF55}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"Error was handled."</nowiki>|#FFB100}}: {{Cl|PRINT}}
{{Cl|ON ERROR}} {{Cl|GOTO}} {{Text|0|#F580B1}}
{{Cl|COLOR}} {{Text|9|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Handler disabled."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|PRINT}} {{Text|<nowiki>"The next error will no longer be handled, press any key ..."</nowiki>|#FFB100}}: {{Cl|SLEEP}}
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Cl|END}}
{{Cl|END}}


hand:
mainHandler:
{{Cl|PRINT}} "got error!"
{{Cl|COLOR}} {{Text|10|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Main handler starts handling error"</nowiki>|#FFB100}}; {{Cl|ERR}}; {{Text|<nowiki>"on program file line"</nowiki>|#FFB100}}; {{Cl|_ERRORLINE}};
{{Cl|BEEP}}: {{Cl|PRINT}} {{Text|<nowiki>"... done."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|RESUME}} {{Cl|NEXT}}
subHandler:
{{Cl|COLOR}} {{Text|12|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Sub handler starts handling error"</nowiki>|#FFB100}}; {{Cl|ERR}}; {{Text|<nowiki>"on program file line"</nowiki>|#FFB100}}; {{Cl|_ERRORLINE}};
{{Cl|BEEP}}: {{Cl|PRINT}} {{Text|<nowiki>"... done."</nowiki>|#FFB100}}: {{Cl|COLOR}} {{Text|7|#F580B1}}
{{Cl|RESUME}} {{Cl|NEXT}}
{{Cl|RESUME}} {{Cl|NEXT}}


{{Cl|SUB}} s
{{Cl|SUB}} {{Text|QB64peErrorTest|#55FF55}}
{{Cl|ON ERROR}} {{Cl|GOTO}} hand
{{Text|<nowiki>'the next line overrides the currently active "mainHandler"</nowiki>|#919191}}
{{Cl|ERROR}} 1
{{Text|<nowiki>'with our new "subHandler", but the _NEWHANDLER keyword explicitly</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} 0
{{Text|<nowiki>'designates this as override and to remember the former handler</nowiki>|#919191}}
{{Cl|PRINT}} "Done!"
{{Cl|ON ERROR}} {{Cl|GOTO}} {{Cl|_NEWHANDLER}} subHandler
{{Cl|END SUB}} '' ''
{{Cl|ERROR}} {{Text|7|#F580B1}}
{{Text|<nowiki>'now the following _LASTHANDLER syntax can easily restore back</nowiki>|#919191}}
{{Text|<nowiki>'to the former handler without we need to know which one it was,</nowiki>|#919191}}
{{Cl|ON ERROR}} {{Cl|GOTO}} {{Cl|_LASTHANDLER}}
{{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.
{{OutputStart}}
{{Ot|Main handler starts handling error 7 on program file line 4 ... done.|#55ff55}}
Error was handled.
 
{{Ot|Sub handler starts handling error 7 on program file line 30 ... done.|#ff5555}}
Error was handled.
 
{{Ot|Main handler starts handling error 7 on program file line 8 ... done.|#55ff55}}
Error was handled.
 
{{Ot|Handler disabled.|#5555ff}}
The next error will no longer be handled, press any key ...
{{OutputEnd}}
{{Small|Examples by RhoSigma}}




Line 64: Line 190:
* [[ON...GOTO]]
* [[ON...GOTO]]
* [[_ERRORLINE]], [[_INCLERRORLINE]], [[_INCLERRORFILE$]]
* [[_ERRORLINE]], [[_INCLERRORLINE]], [[_INCLERRORFILE$]]
* [[ERROR]] {{text|(simulates an error)}}
* [[ERROR]]
* [[ERROR Codes]]
* [[ERROR Codes]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:34, 18 November 2024

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


Syntax

Legacy QuickBASIC/QBasic
ON ERROR GOTO {lineNumberOrLabel | 0}
QB64-PE extension
ON ERROR GOTO {_LASTHANDLER | _NEWHANDLER lineNumberOrLabel}


Parameters

  • lineNumberOrLabel must be a line number or program label in the main part of your program usually placed after the END or SYSTEM statement. Line numbers or labels inside of SUB or FUNCTION blocks are not allowed.
  • 0 (zero) will disable all program based error handling, i.e. errors remain unhandled.


Description

  • An unhandled error in a program will cause execution to stop and an error message box 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 code section or in SUB or FUNCTION procedures, but the line number or label must always be in the main code section.
  • ON ERROR statements take precedence in the order they are encountered, i.e. the most recently set handler is used. Subsequent ON ERROR statements will override the previous one.
  • ON ERROR GOTO 0 can be used to disable program based error trapping and give default error message boxes.
  • GOTO is required in the statement. Cannot use GOSUB.
  • QB64 and QB64-PE do not support the PDS (QuickBASIC 7) ON ERROR RESUME NEXT statement.

  • Since QB64-PE v3.14.1 the _NEWHANDLER and _LASTHANDLER keywords can be used to build up a history chain of the used error handlers. This is especially useful inside SUB or FUNCTION code provided in external libraries, when the handlers are unknown (see Example 3).
    • Works also over several nested or recursive sub-/function calls, but care must be taken for correct exit conditions to avoid crashes due to extensive memory usage. Each use of _NEWHANDLER should have a matching use of _LASTHANDLER.
    • Organize your code so that _NEWHANDLER is not executed 1000s of times in a tight loop, best use is at the entry point of a SUB or FUNCTION. Place _LASTHANDLER at the end point, but make sure it's also called when using EXIT SUB or EXIT FUNCTION.
    • Memory crashes may only happen on unbalanced use of _NEW-/_LASTHANDLER, if you have much more (several 10000s) _NEWHANDLER than _LASTHANDLER executions, so if you take the care and follow given recommendations, then everything will just work fine.
    • The use of _LASTHANDLER is always save. If the history chain has no more enries, then it is synonymous to the zero(0) parameter of the legacy syntax, i.e. disabling program based error trapping.
    • The use of the legacy ON ERROR GOTO 0 will also completly reset/clear the handler history chain.


Availability

  • The _NEWHANDLER and _LASTHANDLER functionality is introduced in QB64-PE v3.14.1.


Examples

Example 1
Using an error handler that prints the error and then continues with the next program line.
'install our own error handler
ON ERROR GOTO errHandler
'now simulate an "Out of Memory" error
ERROR 7
'our error handler will return to this point
PRINT "Error was handled.": PRINT
'now we disable our error handler
ON ERROR GOTO 0
COLOR 9: PRINT "Handler disabled.": COLOR 7
'without our own error handler the following error is reported
'in the usual message box popup again
PRINT "The next error will no longer be handled, press any key ...": SLEEP
ERROR 7
END

'the error handler label must be part of the main program, local
'labels inside a SUB/FUNCTION are not allowed for error handlers
errHandler:
COLOR 10: PRINT "Start handling error"; ERR; "on program file line"; _ERRORLINE;
BEEP: PRINT "... done.": COLOR 7
RESUME NEXT 'returns execution to the code following the error
Start handling error 7 on program file line 6 ... done.
Error was handled.

Handler disabled.
The next error will no longer be handled, press any key ...

Example 2
Using an alternative error handler in a SUB procedure.
ON ERROR GOTO mainHandler
ERROR 7
PRINT "Error was handled.": PRINT
LegacyErrorTest
PRINT "Error was handled.": PRINT
ERROR 7
PRINT "Error was handled.": PRINT
ON ERROR GOTO 0
COLOR 9: PRINT "Handler disabled.": COLOR 7
PRINT "The next error will no longer be handled, press any key ...": SLEEP
ERROR 7
END

'the error handler label must be part of the main program, local
'labels inside a SUB/FUNCTION are not allowed for error handlers
mainHandler:
COLOR 10: PRINT "Main handler starts handling error"; ERR; "on program file line"; _ERRORLINE;
BEEP: PRINT "... done.": COLOR 7
RESUME NEXT
'an alternative error handler which we set from within a SUB/FUNCTION
'however, the error handler label must still be part of the main program
subHandler:
COLOR 12: PRINT "Sub handler starts handling error"; ERR; "on program file line"; _ERRORLINE;
BEEP: PRINT "... done.": COLOR 7
RESUME NEXT

SUB LegacyErrorTest
'the next line overrides the currently active "mainHandler"
'with our new "subHandler"
ON ERROR GOTO subHandler
ERROR 7
'unfortunately the legacy QuickBASIC/QBasic syntax has no function to
'restore back to the old handler, so you need to know which handler was
'active and restore that manually
ON ERROR GOTO mainHandler
END SUB
Main handler starts handling error 7 on program file line 4 ... done.
Error was handled.

Sub handler starts handling error 7 on program file line 33 ... done.
Error was handled.

Main handler starts handling error 7 on program file line 8 ... done.
Error was handled.

Handler disabled.
The next error will no longer be handled, press any key ...

Example 3
Same as example 2, but using QB64-PE specific extensions _NEWHANDLER and _LASTHANDLER.
ON ERROR GOTO mainHandler
ERROR 7
PRINT "Error was handled.": PRINT
QB64peErrorTest
PRINT "Error was handled.": PRINT
ERROR 7
PRINT "Error was handled.": PRINT
ON ERROR GOTO 0
COLOR 9: PRINT "Handler disabled.": COLOR 7
PRINT "The next error will no longer be handled, press any key ...": SLEEP
ERROR 7
END

mainHandler:
COLOR 10: PRINT "Main handler starts handling error"; ERR; "on program file line"; _ERRORLINE;
BEEP: PRINT "... done.": COLOR 7
RESUME NEXT
subHandler:
COLOR 12: PRINT "Sub handler starts handling error"; ERR; "on program file line"; _ERRORLINE;
BEEP: PRINT "... done.": COLOR 7
RESUME NEXT

SUB QB64peErrorTest
'the next line overrides the currently active "mainHandler"
'with our new "subHandler", but the _NEWHANDLER keyword explicitly
'designates this as override and to remember the former handler
ON ERROR GOTO _NEWHANDLER subHandler
ERROR 7
'now the following _LASTHANDLER syntax can easily restore back
'to the former handler without we need to know which one it was,
ON ERROR GOTO _LASTHANDLER
END SUB
Main handler starts handling error 7 on program file line 4 ... done.
Error was handled.

Sub handler starts handling error 7 on program file line 30 ... done.
Error was handled.

Main handler starts handling error 7 on program file line 8 ... done.
Error was handled.

Handler disabled.
The next error will no longer be handled, press any key ...
Examples by RhoSigma


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link