ON ERROR

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

Syntax:
ON ERROR ABORT
ON ERROR IGNORE
ON ERROR SKIP [nn]
ON ERROR CLEAR

Description:

This controls the action taken if an error occurs while running a program and applies to all errors discovered by MMBasic including syntax errors, wrong data, missing hardware, SD Card access, etc.

ON ERROR ABORT will cause MMBasic to display an error message, abort the program and return to the command prompt. 
This is the normal behaviour and is the default when a program starts running.

ON ERROR IGNORE will cause any error to be ignored.
ON ERROR IGNORE can make it very difficult to debug a program so it is strongly recommended that only ON ERROR SKIP be used.

ON ERROR SKIP will ignore an error in a number of commands (specified by the number 'nn') executed following this command. 'nn' is optional, the default if not specified is one. 
After the number of commands has completed (with an error or not) the behaviour of MMBasic will revert to ON ERROR ABORT.

If an error occurs and is ignored/skipped the read only variable MM.ERRNO will be set to non zero and MM.ERRMSG$ will be set to the error message that would normally be generated. 
These are reset to zero and an empty string by ON ERROR CLEAR. They are also cleared when the program is run and when ON ERROR IGNORE and ON ERROR SKIP are used.

 ON ERROR SKIP 1
 x = k/0
 y = g/0
 z = g/0

The first divide by zero is skipped and the second line throws an error.

 ON ERROR SKIP 1
 x = k/0 : y = g/0
 z = g/0

Each statement on the second line is counted towards the 'skip' count. The second statement will cause an error.

 t$ = "999"
 ON ERROR SKIP 1
 x = VAL(t$)/0
 y = g/0
 z = g/0

With built-in functions, the whole statement is treated as one. The first error trapped is on the following line.
(This may not be true for all built-in functions or complex lines.)

 t$ = "999"
 ON ERROR SKIP 1
 x = myval(t$)/0
 y = g/0
 z = g/0
 
FUNCTION myval(txt$)
 myval = VAL(txt$)
END FUNCTION

With user functions, the call to the function is (more than) one statement and the remainder of line 4 is another statement hence the error on that line. 
The example uses up 3 'skip' statements on the one line.

 ON ERROR SKIP 1
' comments are not counted as statements.
 x = k/0
 y = g/0
 z = g/0

Comments are usually ignored but some versions of MMBasic will count comments as statements.

For maximum portability, it is recommended to avoid comments within the range of ON ERROR SKIP.
It is also advisable to limit the number of statements skipped and separate the statements over a number of ON ERROR SKIP commands.

Last edited: 20 August, 2023