QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

Demo of ON ERROR to detect files

February 2 2002 at 5:34 PM
Mac  (Premier Login iorr5t)
Forum Owner


Response to Function to Test for the Existence of a File

 

FAQ014 = How can I trap errors using ON ERROR?

The following shows GetMyErr, a recommended standard error routine to appear in all main programs. When executing an instruction that might cause an error you want to trap, use this sequence:
          MyErr = 0: ON ERROR GOTO GetMyErr
          (whatever instructions you want to trap errors in)
          ON ERROR GOTO 0
The latter is important otherwise any error of any kind in the rest of the program will be ignored unless you check MyErr

If the instruction to be tested is in a SUB or FUNCTION, then use
DIM SHARED MyErr
in the main program.

Mac

CLS
Main:
DO
  ff = FREEFILE
  LINE INPUT "File: "; f$: IF f$ = "" THEN SYSTEM
  MyErr = 0
  ON ERROR GOTO GetMyErr: OPEN f$ FOR INPUT AS #ff
  ON ERROR GOTO 0: CLOSE
  IF MyErr > 0 THEN PRINT "Cannot open "; f$
LOOP WHILE MyErr > 0
PRINT "No problem with "; f$
SYSTEM
GetMyErr:
MyErr = ERR
RESUME NEXT


    
This message has been edited by iorr5t on Nov 21, 2003 12:08 PM


 
 Respond to this message   
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement