Q(uick)BASIC Statement: ERROR

Quick View

ERROR

An error-trapping statement that simulates the occurrence of a BASIC error or allows the user to define error codes

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ERROR expression%
Description/Parameter(s)
expression% The error code of a Basic or user-defined error; a value in the range 1 through 255. To define your own error, use a value that isn't listed in the Basic Run-Time Error Codes table.
Example

The ERRDEV function programming example illustrates the use of ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME.

Syntax
  • ERROR integerexpression
Description/Parameter(s)

The integerexpression represents the error code. It must be greater than 0 and less than or equal to 255. If the integerexpression is an error code already used by BASIC, then the ERROR statement simulates the occurrence of that error and prints the corresponding error message.

To define your own error code, use a value that is greater than any used by the standard BASIC error codes. (Start at 255 and work down to maintain compatibility with future Microsoft BASIC error codes.)

If an ERROR statement specifies a code for which no error message has been defined, the message "Unprintable error" is printed. Executing an ERROR statement for which there is no error-handling routine causes an error message to be printed and execution to halt.

Example

This example uses an ERROR statement in an error-handling routine, Handler, to trap a user input error.
Tip: You must supply a text file when you run this example. Use a text file you have already created, create a file with a text editor, or specify the README.DOC text file.

ON ERROR GOTO Handler OpenFile: INPUT "Name of file to update"; FileSpec$ IF FileSpec$ = "" THEN END OPEN FileSpec$ FOR INPUT AS #1 PRINT "The first five lines of "; FILESPEC$;" are:" : PRINT FOR I = 1 TO 5 LINE INPUT #1, Temp$ PRINT Temp$ NEXT PRINT : INPUT "Is this the correct file"; R$ 'Define error 200. IF LEFT$(R$,1) <> "y" THEN ERROR 200 END Handler: 'Error-handling routine. Number = ERR 'If program generates run-time error for "file not found," 'a special message is printed and the user is prompted to 'enter a new file specification or end the program. IF Number = 53 THEN CLOSE #1 PRINT "File not in this directory" PRINT "Enter new file spec ([d:]xxx...xxx) or" PRINT "press <RETURN> to end program" RESUME OpenFile ELSEIF Number = 200 THEN 'User entered "n" CLOSE #1 RESUME OpenFile ELSE ERROR Number 'Error other than 53 or 200. ON ERROR GOTO 0 'Print message, disable error END IF 'handling, and stop program.
Syntax
  • ERROR integerexpression%
Description/Parameter(s)

Usage Notes

  • If integerexpression% is an error code already used by BASIC, the ERROR statement simulates the occurrence of that error.
  • To define your own error code, use a value that is greater than any used by the standard BASIC error codes. (Start by using error code 255 and work down to avoid compromising compatibility with future Microsoft BASIC error codes.) In general, the error codes used by BASIC are between 1 and 100 (although not all these are used).
  • If an ERROR statement is executed when no error-handling routine is enabled, BASIC generates an error message and stops program execution. If the ERROR statement specified an error code that is not used by BASIC, the message "Unprintable error" is generated.
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