Q(uick)BASIC Function: ERR
Quick View
ERL and ERR
Error-trapping functions that return program-specific status information after an error
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- ERR
- ERL
Description/Parameter(s)
- ERR returns the run-time error code for the most recent error.
- ERL returns the line number where the error occurred, or the closest line number before the line where the error occurred.
- ERL does not return line labels. If there are no line numbers in the program, ERL returns 0.
Example
The ⮜ ERRDEV function programming example ⮞ illustrates the use of ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME.
Syntax
- ERR
- ERL
Description/Parameter(s)
- After an error, the function ERR returns the code for the error, and the ERL function returns the line number where the error occurred. Because ERR and ERL return meaningful values only after an error, they are usually used in error-handling routines to determine the error and the corrective action.
- Because ERL and ERR are functions, you cannot use them on the left-hand side of an assignment statement. However, you may indirectly set them with the ERROR statement.
Differences from BASICA
- The ERL function returns only the line number, not line label, located before the line producing the error. If your program has no line numbers, ERL always returns 0.
Example
The ⮜ ON ERROR statement programming example ⮞ uses the ERR function.
See also:
Syntax
- ERR
- ERL
Description/Parameter(s)
Usage Notes
- After an error, the function ERR returns the latest setting of the runtime error code, and the ERL function returns the line number where the error occurred. Because ERR and ERL return meaningful values only after an error, they are usually used in error-handling routines to determine the error and the corrective action.
- The value returned by the ERR function can be directly set by using the ERR statement. Both the values returned by ERR and ERL may be set indirectly with the ERROR statement.
- The ERL function returns only the line number, not line labels, located before the line producing the error. If your program has no line numbers, or there is no line number in the program before the point where the error occurred, ERL returns 0.
- ERL values differ between programs compiled with BC and those run under QBX. In programs compiled with BC, ERL is set to the last numbered line in the source file preceding the line where the error occurred. When QBX detects an error in a procedure, it looks for a line number only within the immediate procedure. If the procedure doesn't contain a numbered line, QBX returns 0 for ERL. You can make your programs run identically with both BC and QBX by always including a line number at the beginning of a procedure where an error might occur.
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