Simulating+RESUME+and+RESUME+NEXT

//by Richard Russell, September 2007//

BBC BASIC doesn't have the **RESUME** and **RESUME NEXT** statements sometimes found in other dialects of BASIC. Although there are alternative (and probably better) ways of achieving a similar effect when writing a program from scratch, their absence can be inconvenient when translating programs from another dialect. This article describes how a very similar effect can be achieved in //BBC BASIC for Windows// (version 5.60a or later).

Firstly let's review what RESUME and RESUME NEXT do. They are intended for use in an error handler (i.e. a routine activated by **ON ERROR**) and cause execution to continue at the statement that resulted in the error (in the case of **RESUME**) or the statement following the one that resulted in the error (in the case of **RESUME NEXT**).

Here is a simple **GW-BASIC** or **QBASIC** program which illustrates the use of RESUME (//don't// try to run it in **BBC BASIC**!):

code format="qbasic" PRINT "Program starting" ON ERROR GOTO 1000 Divisor = 0 PRINT "Attempting division..." PRINT 10 / Divisor PRINT "Program finished" END

1000 PRINT "Error: "; ERR Divisor = 3 RESUME code When run the program generates the following output:

code Program starting Attempting division... Error: 11 3.333333 Program finished code Note that an error occurs (division by zero) because **Divisor** is initially zero, but in the error handler **Divisor** is changed to 3 and the division is repeated, this time successfully.

Here is the same program in //BBC BASIC for Windows// using some sneaky code to reproduce the effect of **RESUME**:

code format="bb4w" PRINT "Program starting" ON ERROR GOTO 1000 Divisor = 0 PRINT "Attempting division..." PRINT 10 / Divisor PRINT "Program finished" END

1000 PRINT "Error: "; ERR Divisor = 3 REM RESUME P% = PAGE WHILE ?P% AND P% + ?P% < !408 P% += ?P% : ENDWHILE GOTO (P%) code This code produces the following output:

code Program starting Attempting division... Error: 18 3.33333333 Program finished code Note that it has correctly re-executed the division statement but //not// the preceding statements.

If we change the **QBASIC** program to use **RESUME NEXT** instead of **RESUME** this is the output we get:

code Program starting Attempting division... Error: 11 Program finished code Here the division isn't repeated, and the RESUME NEXT transfers control to the line.

Here is the equivalent program in //BBC BASIC for Windows//:

code format="bb4w" PRINT "Program starting" ON ERROR GOTO 1000 Divisor = 0 PRINT "Attempting division..." PRINT 10 / Divisor PRINT "Program finished" END

1000 PRINT "Error: "; ERR Divisor = 3 REM RESUME NEXT P% = PAGE REPEAT P% += ?P% : UNTIL ?P% = 0 OR P% > !408 GOTO (P%) code And here is the output:

code Program starting Attempting division... Error: 18 Program finished code There are a couple of respects in which the code replacements shown here don't work exactly the same as the traditional RESUME and RESUME NEXT statements:


 * 1) The BBC BASIC code transfers control to the **line** which caused the error or the following **line**, //not// to the statement causing the error and the following statement.  Therefore it may not behave as required if there is more than one statement on a line.
 * 2) If the error trapping occurs in a function, procedure or loop, **ON ERROR LOCAL** must be used to avoid the stack being cleared down.

The code listed in this article requires //BBC BASIC for Windows// version 5.60a or later, and relies on undocumented behaviour which is not guaranteed to remain compatible in future versions. However it does work correctly with version 5.70a.