User Tools

Site Tools


simulating_20resume_20and_20resume_20next

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!):

        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

When run the program generates the following output:

Program starting
Attempting division...
Error:  11
 3.333333
Program finished

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:

        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%)

This code produces the following output:

Program starting
Attempting division...
Error: 18
3.33333333
Program finished

Note that it has correctly re-executed the division statement but not the preceding “PRINT” statements.

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

Program starting
Attempting division...
Error:  11
Program finished

Here the division isn't repeated, and the RESUME NEXT transfers control to the “PRINT “Program finished”” line.

Here is the equivalent program in BBC BASIC for Windows:

        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%)

And here is the output:

Program starting
Attempting division...
Error: 18
Program finished

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.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
simulating_20resume_20and_20resume_20next.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1