Q(uick)BASIC Statement: RESUME
Quick View
RESUME
An error-trapping statement that continues program execution after an error-trapping routine has been invoked
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- RESUME [{line | NEXT}]
Description/Parameter(s)
line | The label or number of the line where execution resumes. If line is 0 or omitted, execution resumes with the statement that caused the error. |
NEXT | Resumes execution at the statement following the statement that caused the error. |
Example
The ⮜ ERRDEV function programming example ⮞ illustrates the use of ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME.
See also:
Syntax
- RESUME [0]
- RESUME NEXT
- RESUME {linelabel | linenumber}
Description/Parameter(s)
The different forms of the RESUME statement redirect program flow as described in the following list:
Statement | Where Execution Resumes |
RESUME [0] | At the last statement executed in the module containing the error handler that was used. If an active error handler is found in the module where the error occurs, execution resumes with the statement that caused the error. |
RESUME NEXT | At the statement immediately following the last statement executed in the module containing the error handler that was used. If an active error handler is found in the module where the error occurs, execution resumes with the statement immediately following the statement that caused the error. |
RESUME linelabel | At linelabel |
RESUME linenumber | At linenumber |
A RESUME statement that is not in an error-handling routine produces the error message "RESUME without error." Reaching the end of an error-handling routine without finding RESUME produces the error message "No RESUME."
The line specified in a RESUME { linelabel | linenumber } statement must be defined at the module level. As a rule, avoid using a line label with a RESUME statement. Omitting the line label allows your program to continue no matter where the error occurred.
Note: | Programs containing error-handling routines must be compiled with either the /E (On Error) or /X (Resume Next) options when you are compiling from the BC command line. No options are required when compiling in the QuickBASIC environment, or using the Make EXE command from the QuickBASIC Run menu. |
Differences from BASICA
- In BASICA, if an error occurs in a DEF FN function, both RESUME and RESUME NEXT attempt to resume program execution at the line containing the function.
Example
This example has an error-handling routine starting at line 900 which traps negative arguments to the SQR function.
5 CLS
10 ON ERROR GOTO 900
20 FOR I = 4 TO -2 STEP -1
30 PRINT I, 1 - SQR(I)
40 NEXT
50 END
900 'Error-handling routine
910 PRINT "No negative arguments"
920 RESUME NEXT
Sample Output:
4 -1 3 -.7320509 2 -.4142136 1 0 0 1 -1 No negative arguments -2 No negative argumentsSee also:
Syntax
- RESUME { [0] | NEXT | line }
Description/Parameter(s)
Usage Notes
- The location where execution resumes is based on the location of the error handler in which the error is trapped, not on the location where the error occurred.
- The following table summarizes the resumption rules for the RESUME[0] statement:
Error handler | Location of error | Where program resumes |
Local | Same procedure error | Statement that caused |
Module level | Same module error | Statement that caused |
Local or module level | Another procedure, same or another module | Statement that last called out of the procedure OR module that contains the ERROR handler |
- Note: If BASIC had to search for the error handler (the error handler that contains the RESUME statement is in a procedure or module other than the one in which the error occurred), then the last statement executed in that procedure (or module) is the last call out of that procedure or module.
- As a rule, avoid using a line argument with a RESUME statement in a module-level error handler, unless you expect errors to occur only at the module-level.
- A RESUME statement that is not in an error-handling routine produces the error message, "RESUME without error."
- When an error-handling routine is active, if the end of the program text is encountered before executing a RESUME statement, BASIC generates the error message, "No RESUME." This also is true if an END statement (or an END SUB, END FUNCTION, or END DEF statement for a local error handler) is executed before a RESUME.
- Programs that contain error-handling routines must be compiled with either the /E (On Error) or /X (Resume Next) options when you are compiling from the BC command line.
Example
The program uses the ON ERROR statement to enable error trapping. The program attempts to write a large file to the disk. If an error occurs, control is passed to a handling routine, and the ERR function is used to determine an appropriate message to display. The ERROR, ERR, and RESUME statements are used to exit the handling routines, and the END, STOP, and SYSTEM statements demonstrate ways to exit a program.
DECLARE SUB ErrorMessage (Message$)
DECLARE SUB WriteBigFile (Filenum%)
ON ERROR GOTO ErrHandler
CLS
PRINT "This program will attempt to write a large file to a disk drive"
PRINT "you select. The file will be erased when the program ends."
PRINT
DO
INPUT "Which drive"; DR$
DR$ = UCASE$(DR$)
LOOP UNTIL LEN(DR$) >= 1 AND LEN(DR$) <= 2 AND DR$ >= "A" AND DR$ <= "Z"
IF LEN(DR$) > 1 THEN
IF RIGHT$(DR$, 1) <> ":" THEN
DR$ = LEFT$(DR$, 1) + ":"
END IF
ELSE
DR$ = DR$ + ":"
END IF
'Put together a complete file specification.
FileSpec$ = DR$ + "BIGFILE.XXX"
'Get the next available file number.
Filenum% = FREEFILE
'Try to open the file.
OPEN FileSpec$ FOR OUTPUT AS Filenum%
WriteBigFile Filenum%
CLOSE Filenum%
CLS
PRINT "Everything was OK. No errors occurred."
PRINT "Deleting the file that was created."
KILL FileSpec$
'Same as END, returns to operating system.
SYSTEM
ErrHandler:
SELECT CASE ERR
CASE 52 'Bad file name or number.
END
CASE 53 'File not found.
RESUME NEXT
CASE 57 'Device I/O error.
ErrorMessage "You should probably format the diskette."
END
CASE 64 'Bad File Name.
ErrorMessage "The drive name you specified was not correct."
END
CASE 68 'Device unavailable.
ErrorMessage "The drive you named is unavailable."
END
CASE 71 'Drive not ready.
ErrorMessage "The drive was not ready. Check the drive!"
END
CASE ELSE
ErrorMessage "An unexpected FATAL error has occurred."
STOP
END SELECT
SUB ErrorMessage (Message$)
ON LOCAL ERROR GOTO MessageError
CLS
PRINT Message$
PRINT "Cannot continue."
PRINT
PRINT "Press any key to exit."
DO
LOOP WHILE INKEY$ = ""
EXIT SUB
MessageError:
RESUME NEXT
END SUB
SUB WriteBigFile (Filenum%)
ON LOCAL ERROR GOTO LocalHandler
TEXT$ = STRING$(1024, "A")
FOR I% = 1 TO 400
PRINT #Filenum%, TEXT$
NEXT I%
EXIT SUB
LocalHandler:
SELECT CASE ERR
CASE 61 'Disk full.
ErrorMessage ("There is no room remaining on the disk.")
KILL "BIGFILE.XXX"
END
CASE ELSE
ERROR ERR
END SELECT
END SUB
See also: