Author Topic: ON ERROR PUZZLE  (Read 4113 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
ON ERROR PUZZLE
« on: July 23, 2021, 08:37:31 am »
Code: QB64: [Select]
  1.     DO
  2.         CLEAR
  3.         start:
  4.         IF _DIREXISTS(".\RisultatiQB64") THEN
  5.             ON ERROR GOTO cancel
  6.             KILL (".\RisultatiQB64\*.*")
  7.             GOTO endd
  8.             cancel:
  9.             IF _FILEEXISTS(".\RisultatiQB64\*.*") THEN
  10.                 PRINT "Close the file open and press a key": SLEEP
  11.                 GOTO start
  12.             ELSE
  13.                 GOTO endd
  14.             END IF
  15.             endd:
  16.             RMDIR (".\RisultatiQB64")
  17.             ON ERROR GOTO 0
  18.         END IF
  19. CODE

Code: QB64: [Select]
  1. This little sequence is intended to do that: if a preview launch of the program has created the directory of the results,
  2. a second launch must delete it.
  3.  
  4. The sequence works like this:
  5. - line 5: the directory exists?
  6.   NO: The program go to CODE IT WORKS
  7.   YES: go to line 6.
  8. - line 7: are there some files in the directory?
  9.           NO: KILL command gives an error if it tries to delete files that don't exists, so the program go to cancel:
  10.               - line 10: are there some files in the directory?
  11.                           NO: go to end, delete the directory and go to CODE. IT WORKS
  12.           YES (and no file is used): no error, so go to line 8:
  13.               - line 8: go to end, delete the directory and go ahead. IT WORKS
  14.           YES (and a file is in use): there is an error, so the program go to cancel:
  15.                 HERE IS MY PROBLEM:
  16.               - line 10: if there are files in the directory and an error is occurred on KILL command, it is because almost a file is open,
  17.                          so the program says to close it and then goes to start: IT DOESN'T WORK: WHY???
  18.                          line 10 doesn't work as I want, and program goes however to line 17 and crashes.
« Last Edit: July 23, 2021, 08:45:06 am by bartok »

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: ON ERROR PUZZLE
« Reply #1 on: July 26, 2021, 02:42:59 am »
How can I avoid the crash? The program creates, as results, some *.csv files. The program is able to cancel the files and then the directory. If the directory exists, but there are no file, KILLing the files produces an error. So, ON ERROR, it delete the directory if it exists. But, if the user has a *.csv file opened in Excel and he is operating on it, the program fails to delete the file and has an error. I want that ON ERROR, it says to the user to close the program open, and repeat. But it doesn't work. I have tried in many ways.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: ON ERROR PUZZLE
« Reply #2 on: July 28, 2021, 05:38:44 am »
Code: QB64: [Select]
  1. ON ERROR GOTO cancel
  2. IF _DIREXISTS(".\RisultatiQB64") THEN
  3.     IF _FILEEXISTS(".\RisultatiQB64\C.IdrogrammiUnitari.csv") THEN
  4.         DO
  5.             KILL (".\RisultatiQB64\*.*")
  6.             EXIT DO
  7.             cancel:
  8.             PRINT "Close the file opened and press a key.": SLEEP
  9.         LOOP
  10.     END IF
  11.     RMDIR (".\RisultatiQB64")

BOH...
The program creates many files as a result of the calculations, in the directory ".\RisultatiQB64". Between these files, there is "C.IdrogrammiUnitari.csv"

So, in line 3, if "C.IdrogrammiUnitari.csv" doesn't exist, the program go to the line 11 and it deletes the directory ".\RisultatiQB64" because it is empty.
If "C.IdrogrammiUnitari.csv" exists, the program go to line 4 and 5, where it deletes all the files in the directory, then it EXIT DO, and it goes to line 11, and it deletes the directory ".\RisultatiQB64" because it is empty.

If, in the directory, there is a file opened (for example in Excel), line 5 gives an error, so, thank's to the line 1, the program, ON ERROR, goes to "cancel:", where it says to the user to close the file, and press a key. Pressing the key, the program loops.

But at this second time, if I DON'T close the file, the program , at line 5, rather then go (ON ERROR) to "cancel:", as intended, it crashes, as if ON ERROR command of line 1, was not in force. ON ERROR GOTO 0 is outside.

Why at the second loop the program doesn't go ON ERROR to "cancel:"??

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ON ERROR PUZZLE
« Reply #3 on: July 28, 2021, 06:00:03 am »
Hi Bartok

1. about crashing maybe you can solve adding RESUME NEXT after SLEEP in error procedure
see documentation here https://www.qb64.org/wiki/ON_ERROR

2. about opening files already in use by another program... it depends by locking the file in opening.
It happens often when I want to plug off an USB from PC and some not visible Windows processes are working...
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: ON ERROR PUZZLE
« Reply #4 on: July 28, 2021, 08:23:57 am »
Hi Bartok

1. about crashing maybe you can solve adding RESUME NEXT after SLEEP in error procedure
see documentation here https://www.qb64.org/wiki/ON_ERROR

2. about opening files already in use by another program... it depends by locking the file in opening.
It happens often when I want to plug off an USB from PC and some not visible Windows processes are working...

Thank's you! Adding RESUME after SLEEP it works fine.
But it works with only RESUME, not RESUME NEXT nor RESUME cancel:

with RESUME NEXT it works fine only the second time, but not further. With only RESUME it works always as expected.

However, I don't have undestood why it didn't work without RESUME. Why the LOOP was not enough? RESUME resumes what?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ON ERROR PUZZLE
« Reply #5 on: July 28, 2021, 09:11:38 am »
Hi Bartok
I think that RESUME says to the compiler where to jump after the error_manager procedure ends. So without any instruction the situation becomes undefined!
Yes better RESUME just because you put this procedure into a loop and  with RESUME NEXT you say to compiler to jump on EXIT DO. While using RESUME you jump at the instruction that activates the error trap.
Code: QB64: [Select]
  1.  DO
  2.             KILL (".\RisultatiQB64\*.*")
  3.             EXIT DO
  4.             cancel:
  5.             PRINT "Close the file opened and press a key.": SLEEP
  6.         LOOP
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: ON ERROR PUZZLE
« Reply #6 on: July 28, 2021, 03:13:22 pm »
Hi Bartok
I think that RESUME says to the compiler where to jump after the error_manager procedure ends. So without any instruction the situation becomes undefined!
Yes better RESUME just because you put this procedure into a loop and  with RESUME NEXT you say to compiler to jump on EXIT DO. While using RESUME you jump at the instruction that activates the error trap.
Code: QB64: [Select]
  1.  DO
  2.             KILL (".\RisultatiQB64\*.*")
  3.             EXIT DO
  4.             cancel:
  5.             PRINT "Close the file opened and press a key.": SLEEP
  6.         LOOP

The final "release" of the that is:
Code: QB64: [Select]
  1. inizio:
  2. IF _DIREXISTS(".\RisultatiQB64") THEN
  3.     ON ERROR GOTO cancel1
  4.     KILL (".\RisultatiQB64\*.*")
  5.     RMDIR (".\RisultatiQB64")
  6.     GOTO prosegui
  7.     cancel1:
  8.     ON ERROR GOTO cancel2
  9.     RESUME NEXT
  10.     RMDIR (".\RisultatiQB64")
  11.     GOTO prosegui
  12.     cancel2:
  13.     CLS
  14.     BEEP
  15.     PRINT "Un file interno alla directory"
  16.     PRINT CHR$(34); _CWD$; "\RisultatiQB64\"; CHR$(34)
  17.     PRINT "Š aperto in un altro programma."
  18.     PRINT "Per proseguire chiudere il file e premere un tasto."
  19.     SLEEP
  20.     RESUME inizio
  21. prosegui:
  22.  
  23.         DIM SHARED idrogrammi1a24(2, 24, 50, 1) AS idrogramma
  24.         DIM SHARED MassimiIdrogrammi1a24(2, 24, 1) AS idrogramma
  25.  


Your hint to use RESUME was very important. But I don't really undestand what RESUME actually does. For example, why at line 9 is necessary to write "RESUME NEXT", if already line 8 tells to go to cancel2, if there is an error on line 10?

And why on line 20 we have to write RESUME inizio and not GOTO inizio?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ON ERROR PUZZLE
« Reply #7 on: August 01, 2021, 03:11:42 am »
Hi Bartok
I'm not a teacher so my answer is upon my experience...

code is at level 0, error runtime code is at level 1 so also if you have mixed these two levels they are managed in a different way by the compiler.
so your code spaghetti , also if working , can be showed in a more linear mode as following code box
Code: QB64: [Select]
  1. ' prima trappola per gli errori runtime  /first error trap
  2. cancel1:
  3. On Error GoTo cancel2
  4. Resume Next '<----- salta alla riga di codice 23/this jumps to linecode 23
  5.  
  6. ' seconda trappola per gli errori runtime  / second error trap
  7. cancel2:
  8. Print "Un file interno alla directory"
  9. Print Chr$(34); _CWD$; "\RisultatiQB64\"; Chr$(34)
  10. Print "Š aperto in un altro programma."
  11. Print "Per proseguire chiudere il file e premere un tasto."
  12. RmDir (".\RisultatiQB64")
  13. Resume inizio
  14.  
  15. ' procedura eliminazione files / killing files procedure
  16. inizio:
  17. If _DirExists(".\RisultatiQB64") Then
  18.     On Error GoTo cancel1
  19.     Kill (".\RisultatiQB64\*.*")
  20.     RmDir (".\RisultatiQB64")
  21. ' fine gestione errori / end managing errors
  22.  
  23.       DIM SHARED idrogrammi1a24(2, 24, 50, 1) AS idrogramma
  24.         DIM SHARED MassimiIdrogrammi1a24(2, 24, 1) AS idrogramma
  25.  

I hope that this answer gives you a different point of view on runtime error manager
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: ON ERROR PUZZLE
« Reply #8 on: August 02, 2021, 04:45:18 am »
Hi Bartok
I'm not a teacher so my answer is upon my experience...

code is at level 0, error runtime code is at level 1 so also if you have mixed these two levels they are managed in a different way by the compiler.
so your code spaghetti , also if working , can be showed in a more linear mode as following code box
Code: QB64: [Select]
  1. ' prima trappola per gli errori runtime  /first error trap
  2. cancel1:
  3. On Error GoTo cancel2
  4. Resume Next '<----- salta alla riga di codice 23/this jumps to linecode 23
  5.  
  6. ' seconda trappola per gli errori runtime  / second error trap
  7. cancel2:
  8. Print "Un file interno alla directory"
  9. Print Chr$(34); _CWD$; "\RisultatiQB64\"; Chr$(34)
  10. Print "Š aperto in un altro programma."
  11. Print "Per proseguire chiudere il file e premere un tasto."
  12. RmDir (".\RisultatiQB64")
  13. Resume inizio
  14.  
  15. ' procedura eliminazione files / killing files procedure
  16. inizio:
  17. If _DirExists(".\RisultatiQB64") Then
  18.     On Error GoTo cancel1
  19.     Kill (".\RisultatiQB64\*.*")
  20.     RmDir (".\RisultatiQB64")
  21. ' fine gestione errori / end managing errors
  22.  
  23.       DIM SHARED idrogrammi1a24(2, 24, 50, 1) AS idrogramma
  24.         DIM SHARED MassimiIdrogrammi1a24(2, 24, 1) AS idrogramma
  25.  

I hope that this answer gives you a different point of view on runtime error manager

yes, thank's you TempodiBasic. I have undestood how RESUME works. Thank's.

PS: ora che mi viene in mente... ma "Tempo di Basic" era una rivista del Commodor giusto?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ON ERROR PUZZLE
« Reply #9 on: August 02, 2021, 06:25:14 pm »
it is possible that Tempo di Basic was a Commodore 64 newspaper, but I never got Commodor64 or VIC20 or Amiga500.
I have had my first contact with programming and Qbasic on my first notebook (monitor grayscale) in 1994! And it was love at first sight.
Programming isn't difficult, only it's  consuming time and coffee