QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: bryandj23 on June 25, 2020, 11:53:46 am

Title: Trapping "File Not Found" Errors
Post by: bryandj23 on June 25, 2020, 11:53:46 am
Hey all.   Long time QB45 user, and jumping on the QB64 bandwagon like everyone else.

I'm trying to trap the 'file not found' error, and running into an issue upon subsequent error conditions.

Code: QB64: [Select]
  1. GetFileName:
  2. Line Input InFile$
  3. On Error GOTO NotFound
  4. Open InFile$ for INPUT AS #1
  5.  
  6. ...
  7.  
  8.  

then, NotFound:

Code: QB64: [Select]
  1. NotFound:
  2. COLOR 15: PRINT Infile$; " ";: COLOR 12: PRINT "Not Found!!"
  3. GOTO getfilename


This routine correct traps the first occurrence, however on second occurrence, the trap does not work and QB64 attempts to open a non-existent file and I get a pop-up error (assuming from the compiler).

I'm sure I'm missing something simple (like clearing the error state), but figured the 'ON ERROR GOTO 0" would handle it properly.

Thanks in advance.

Title: Re: Trapping "File Not Found" Errors
Post by: Petr on June 25, 2020, 12:12:32 pm
add RESUME


first create file test.txt and then run it:


Code: QB64: [Select]
  1. ON ERROR GOTO handler
  2. LINE INPUT #1, value$
  3. PRINT value$
  4.  
  5. handler:
  6. PRINT "Error:"; ERR
  7. OPEN "test.txt" FOR INPUT AS #1
  8.  
Title: Re: Trapping "File Not Found" Errors
Post by: bplus on June 25, 2020, 12:23:19 pm
You can save yourself from trouble by checking if _FILEEXISTS first before trying to open it.
Title: Re: Trapping "File Not Found" Errors
Post by: SpriggsySpriggs on June 25, 2020, 12:31:19 pm
You can save yourself from trouble by checking if _FILEEXISTS first before trying to open it.
Exactly. Check for the file existing first. You don't want to invoke an error if you don't have to. Just do this:
Modification: Use BINARY instead of INPUT for much faster reading of a file.
Code: QB64: [Select]
  1.   'OPEN InFile$ FOR INPUT AS #1
  2.   OPEN InFile$ FOR BINARY AS #1
  3.   LINE INPUT #1, value$
  4.   COLOR 15: PRINT Infile$; " ";: COLOR 12: PRINT "Not Found!!"
Title: Re: Trapping "File Not Found" Errors
Post by: doppler on June 25, 2020, 03:11:09 pm
False positive if the directory does not exists so
_DIREXISTS(filepath$)
Title: Re: Trapping "File Not Found" Errors
Post by: TempodiBasic on June 25, 2020, 04:58:20 pm
Hi bryandj23

1. I agree with other QB64 coders, better to use _FILEEXSISTS than ON ERROR GOTO .... RESUME

2. you get one run of error trapping because you disable the error handler
read help on wiki about ON ERROR GOTO  https://www.qb64.org/wiki/ON_ERROR (https://www.qb64.org/wiki/ON_ERROR)
as you can read ON ERROR GOTO 0 disables the error Handler!
To let you run your code you must REM the line on ERROR GOTO 0
Moreover to reset the error trapping you must return from ErrorTrappingSub_Function using RESUME (that triggers again the error) or RESUME NEXT (that activates the next line of code after the ErrorTrigger)

Her how you can work your code
Code: QB64: [Select]
  1. GetFileName:
  2. COLOR 7, 0
  3. PRINT "write a file to open"
  4. LINE INPUT InFile$
  5. ON ERROR GOTO NotFound ' this activates Error Trapping
  6. OPEN InFile$ FOR INPUT AS #1
  7. IF InFile$ = "" GOTO GetFileName
  8. PRINT " File opened..."
  9.  
  10. NotFound:
  11. COLOR 15: PRINT InFile$; " ";: COLOR 12: PRINT "Not Found!!"
  12. 'ON ERROR GOTO 0 '<---- this disables Error Trapping
  13. InFile$ = "" ' our flag to repeat the INPUT of filename
  14. RESUME NEXT ' this activates the next line to that triggers the error trap
  15.  
  16.  

but at the place of old ways you can use QB64's news
Code: QB64: [Select]
  1. GetFileName:
  2. COLOR 7, 0
  3. PRINT "write a file to open"
  4. LINE INPUT InFile$
  5. IF _FILEEXISTS(InFile$) THEN OPEN InFile$ FOR INPUT AS #1 ELSE COLOR 15: PRINT InFile$; " ";: COLOR 12: PRINT "Not Found!!": GOTO GetFileName
  6. PRINT " File opened..."
  7.  

Good Coding