qb45advr.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
ON ERROR Statement Programming Example
  QuickSCREEN      Details     Example      Contents    Index
──────────────────────────────────────────────────────────────────────────────
ON ERROR Statement Programming Example
 
The following program gets a file name from the user and displays the
file on the screen. If the file cannot be opened, an error-handling
routine traps the error and starts the program again at the prompt
for the file name.
 
DEFINT A-Z
 
' establish the error-handling routine
ON ERROR GOTO ErrorHandler
 
CLS
' get a file name
INPUT "Enter the file to display: ",filename$
' open the file
OPEN filename$ FOR INPUT AS #1
 
' display the file on the screen
DO WHILE NOT EOF(1)
   LINE INPUT #1, aline$
   PRINT aline$
LOOP
END
'
' error handling routine handles only "Bad File Name";
' aborts on any other error
'
CONST BADFILENAME = 53
 
ErrorHandler:
   IF ERR = BADFILENAME THEN
      ' get another file name
      PRINT "File " UCASE$(filename$) " not found."
      INPUT "Enter the file to display: ",filename$
      RESUME
   ELSE
      ' some other error, so print message and abort
      PRINT "Unrecoverable error--"; ERR
      ON ERROR GOTO 0
   END IF