qb45advr.hlp (Topic list)
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