Author Topic: Trapping "File Not Found" Errors  (Read 3770 times)

0 Members and 1 Guest are viewing this topic.

Offline bryandj23

  • Newbie
  • Posts: 1
    • View Profile
Trapping "File Not Found" Errors
« 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.


Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Trapping "File Not Found" Errors
« Reply #1 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.  
« Last Edit: June 25, 2020, 12:19:44 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Trapping "File Not Found" Errors
« Reply #2 on: June 25, 2020, 12:23:19 pm »
You can save yourself from trouble by checking if _FILEEXISTS first before trying to open it.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Trapping "File Not Found" Errors
« Reply #3 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!!"
« Last Edit: June 25, 2020, 02:38:47 pm by SpriggsySpriggs »
Shuwatch!

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: Trapping "File Not Found" Errors
« Reply #4 on: June 25, 2020, 03:11:09 pm »
False positive if the directory does not exists so
_DIREXISTS(filepath$)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Trapping "File Not Found" Errors
« Reply #5 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
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
Programming isn't difficult, only it's  consuming time and coffee