Author Topic: On Error - Resume  (Read 3018 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
On Error - Resume
« on: June 07, 2019, 11:51:03 am »
The On Error, error trapping - Resume, or Return, even using a line number, isn't working for me in qb64. Must admit I have only used it before in QBasic but the wiki seems to suggest it still does work in qb64. Not only can I not get it to return or resume (by the way,it will go to the label and perform some error info routine) but my computer freezes with a blank white square in the middle of the screen. I can't get back to the IDE with either Esc or Pause/Break key (I'm using 64 bit). I have to restart my computer. In the program I'm writing I'm expecting an error however trying to use an error trapping routine to step over the error once it occurs and carry on with the run of the program.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: On Error - Resume
« Reply #1 on: June 07, 2019, 02:02:30 pm »
Hi Dimster
can you give more info?
the wiki example works fine

see attachment
  [ You are not allowed to view this attachment ]  
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: On Error - Resume
« Reply #2 on: June 07, 2019, 03:05:53 pm »
Hello Tempo - love that handle
I'm not sure how you were able to add "Next" to Resume. When I try that I get an error. The best I seem to be able to do is use Resume or Return in the error handling routine but it actually doesn't resume or return to the program. My actual program is over closing in on 13000 lines, so there is likely something going on in there which is creating more of an adverse effect than the following code provides. In my actual program I do not not get the Error box which states "Return without a Gosub" and the choice to "continue or not". I just get the empty white box and a frozen computer.

But here is an example of the coding in my program. When you run this you get the Error Handling routine working but in the actual coding, after the error trapping is a further PRINT statement or 2. I'm expecting the Error Handling routine to pass the run back to continue the run of the program and eventually PRINT out the array. That example coding here ends the run.

DIM TestArray(1 TO 50, 1 TO 45)


FOR x = 1 TO 50
    a = a + 1
    FOR y = 1 TO 45
        b = b + 1
        TestArray(x, y) = TestArray(x, y) + (a + b)
    NEXT y
NEXT x



FOR x = 1 TO 50
    FOR y = 1 TO 44
        ON ERROR GOTO HandlingTestArray
        TotalTestArray(x, y) = TotalTestArray(x, y) + TestArray(x, y)

        WRITE #1, TestArray(x, y)
        ON ERROR GOTO 0

    NEXT y
NEXT x
CLOSE 1



FOR x = 1 TO 50
    FOR y = 1 TO 45

        PRINT "Total Value of the Array  is now "; TotalTestArray(x, y)
    NEXT y
NEXT x

END

HandlingTestArray:
CLS
SCREEN _NEWIMAGE(1200, 800, 32)
_FULLSCREEN

PRINT "Error is "; ERR
PRINT "Error line is "; _ERRORLINE
PRINT
PRINT
PRINT "The Error involves x equaling "; x; " and y equaling "; y; " placing the value of "; TestArray(x, y); " in TestArray("; x; ","; y; ")"
SLEEP
'RESUME
RETURN

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: On Error - Resume
« Reply #3 on: June 07, 2019, 03:46:46 pm »
You have two errors and apparent disaster when switch screens in error handler!

This works with RESUME
Code: QB64: [Select]
  1. DIM TestArray(1 TO 50, 1 TO 45)
  2.  
  3. 'uncomment next line to get through first error handler
  4. 'DIM totalTestArray(1 TO 50, 1 TO 45) 'this is not yet DIM'd !!!!!!!!!!!!!!!!!!!!!!!
  5.  
  6. FOR x = 1 TO 50
  7.     a = a + 1
  8.     FOR y = 1 TO 45
  9.         b = b + 1
  10.         TestArray(x, y) = TestArray(x, y) + (a + b)
  11.     NEXT y
  12.  
  13.  
  14.  
  15. FOR x = 1 TO 50
  16.     FOR y = 1 TO 44
  17.         ON ERROR GOTO HandlingTestArray
  18.         totalTestArray(x, y) = totalTestArray(x, y) + TestArray(x, y)
  19.  
  20.  
  21.         ' the 2nd error is that no file was opened
  22.         '52 bad file error because hven't started one yet!!!
  23.         'WRITE #1, TestArray(x, y)
  24.         'ON ERROR GOTO 0
  25.  
  26.     NEXT y
  27.  
  28.  
  29.  
  30. FOR x = 1 TO 50
  31.     FOR y = 1 TO 45
  32.  
  33.         PRINT "Total Value of the Array  is now "; totalTestArray(x, y)
  34.     NEXT y
  35.  
  36.  
  37. HandlingTestArray:
  38. 'SCREEN _NEWIMAGE(1200, 800, 32)  '<<<<<<<<<<<<<<<<<<<<< holy ship comment this out!!!!!!!!!!!!!!!!!
  39. '_FULLSCREEN                      ' and this!!!!!!!!!!!!!!
  40.  
  41. PRINT "Error is "; ERR
  42. PRINT "Error line is "; _ERRORLINE
  43. PRINT "The Error involves x equaling "; x; " and y equaling "; y; " placing the value of "; TestArray(x, y); " in TestArray("; x; ","; y; ")"
  44.  
  45. 'SLEEP
  46. RESUME '<<<<<<<<<<<<<<<<<<<<<<<<<< now this is OK
  47. 'RETURN
  48.  
  49.  

I think your theory of the problem is wrong, the errors were from undimensioned array and no #1 file to write to.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: On Error - Resume
« Reply #4 on: June 07, 2019, 03:59:22 pm »
Hi. First repaired your code:

in your code miss opening file for output and TotalTestArray is not declared.

Code: QB64: [Select]
  1.  
  2. DIM TestArray(1 TO 50, 1 TO 45)
  3. DIM TotalTestArray(1 TO 50, 1 TO 45)
  4.  
  5.  
  6. FOR x = 1 TO 50
  7.     a = a + 1
  8.     FOR y = 1 TO 45
  9.         b = b + 1
  10.         TestArray(x, y) = TestArray(x, y) + (a + b)
  11.     NEXT y
  12.  
  13.  
  14. OPEN "ttttt" FOR OUTPUT AS #1
  15. FOR x = 1 TO 50
  16.     FOR y = 1 TO 44
  17.         REM        ON ERROR GOTO HandlingTestArray
  18.         TotalTestArray(x, y) = TotalTestArray(x, y) + TestArray(x, y)
  19.  
  20.         WRITE #1, STR$(TestArray(x, y))
  21.         ON ERROR GOTO 0
  22.  
  23.     NEXT y
  24.  
  25.  
  26.  
  27. FOR x = 1 TO 50
  28.     FOR y = 1 TO 45
  29.  
  30.         PRINT "Total Value of the Array  is now "; TotalTestArray(x, y)
  31.     NEXT y
  32.  
  33.  
  34. HandlingTestArray:
  35. SCREEN _NEWIMAGE(1200, 800, 32)
  36.  
  37. PRINT "Error is "; ERR
  38. PRINT "Error line is "; _ERRORLINE
  39. PRINT "The Error involves x equaling "; x; " and y equaling "; y; " placing the value of "; TestArray(x, y); " in TestArray("; x; ","; y; ")"
  40. 'RESUME
  41.  

And here my attemp for your problem: How use on error / resume:

Code: QB64: [Select]
  1. ON ERROR GOTO ErrorHandler
  2. PRINT "Opening file, which not exists for reading...(ERR = 53)"
  3. OPEN "File_which_not_exists.txt" FOR INPUT AS #1
  4. LINE INPUT #1, text$
  5. PRINT text$
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. ErrorHandler:
  16.     CASE 53
  17.         f = FREEFILE
  18.         OPEN "File_which_not_exists.txt" FOR OUTPUT AS #f
  19.         PRINT #f, "File already created in error handler"
  20.         CLOSE f
  21.  



Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: On Error - Resume
« Reply #5 on: June 07, 2019, 04:05:38 pm »
Ha, Bplus is faster!  RESUME NEXT is for cases, if error event is solved later, so error handler can detect next errors.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: On Error - Resume
« Reply #6 on: June 07, 2019, 09:03:11 pm »
Thank you bplus - I had 2 deliberate errors in the code to trigger the Error Trap. The first as you pointed out was failure to Open the file for input and my second was the looping count which shows up on line 17. ( ie for y = 1 to 44, rather than 1 to 45). I had not realized TotalTestArray had not been Dim'd. But even so, it would seem you HAVE to fix all the problems before you can get lines 33 -38 to run. I was expecting Resume or Resume Next of Return to just jump over the problem and execute lines 33 - 38 - just print what it had without correcting any code.


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: On Error - Resume
« Reply #7 on: June 07, 2019, 09:39:58 pm »
Thank you bplus - I had 2 deliberate errors in the code to trigger the Error Trap. The first as you pointed out was failure to Open the file for input and my second was the looping count which shows up on line 17. ( ie for y = 1 to 44, rather than 1 to 45). I had not realized TotalTestArray had not been Dim'd. But even so, it would seem you HAVE to fix all the problems before you can get lines 33 -38 to run. I was expecting Resume or Resume Next of Return to just jump over the problem and execute lines 33 - 38 - just print what it had without correcting any code.

Hi Dimster,

Think about it, how can you resume execution of code and run further down the program without fixing the problem? The error handler attempts to fix a problem or just reports for code fix later.

You know, I don't think running 1 short on the y in that one loop ( FOR y = 1 TO 44 ) will cause Execution problems that would generate errors, the array will just hold zero's, if it were big enough. :)

Runs without stopping for ERROR
Code: QB64: [Select]
  1. DIM TestArray(1 TO 50, 1 TO 45)
  2.  
  3. 'uncomment next line to get through first error handler
  4. DIM totalTestArray(1 TO 50, 1 TO 45) 'this is not yet DIM'd !!!!!!!!!!!!!!!!!!!!!!!
  5.  
  6. FOR x = 1 TO 50
  7.     a = a + 1
  8.     FOR y = 1 TO 45
  9.         b = b + 1
  10.         TestArray(x, y) = TestArray(x, y) + (a + b)
  11.     NEXT y
  12.  
  13.  
  14.  
  15. FOR x = 1 TO 50
  16.     FOR y = 1 TO 44
  17.         ON ERROR GOTO HandlingTestArray
  18.         totalTestArray(x, y) = totalTestArray(x, y) + TestArray(x, y)
  19.  
  20.  
  21.         ' the 2nd error is that no file was opened
  22.         '52 bad file error because hven't started one yet!!!
  23.         'WRITE #1, TestArray(x, y)
  24.         'ON ERROR GOTO 0
  25.  
  26.     NEXT y
  27.  
  28.  
  29.  
  30. FOR x = 1 TO 50
  31.     FOR y = 1 TO 45
  32.  
  33.         PRINT x, y, "Total Value of the Array  is now "; totalTestArray(x, y)
  34.     NEXT y
  35.  
  36.  
  37. HandlingTestArray:
  38. 'SCREEN _NEWIMAGE(1200, 800, 32)  '<<<<<<<<<<<<<<<<<<<<< holy ship comment this out!!!!!!!!!!!!!!!!!
  39. '_FULLSCREEN                      ' and this!!!!!!!!!!!!!!
  40.  
  41. PRINT "Error is "; ERR
  42. PRINT "Error line is "; _ERRORLINE
  43. PRINT "Ending program after you press any..."
  44. RESUME '<<<<<<<<<<<<<<<<<<<<<<<<<< now this is OK
  45. 'RETURN
  46.  
« Last Edit: June 07, 2019, 09:49:54 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: On Error - Resume
« Reply #8 on: June 08, 2019, 05:07:46 am »
Ya, that is the conclusion I have come to - ON ERROR trapping does not just step over the error but needs to fix it. It would have be a pretty sophisticated Error handling routine to capture and correct ALL the problems with the example code. I guess Petr's point would be one ERROR trapping per problem. Thanks again you guys. Back to the drawing board.