Author Topic: "On Error" - Common Label within a SUB/FUNCTION...  (Read 3839 times)

0 Members and 1 Guest are viewing this topic.

Offline lemonskunnk

  • Newbie
  • Posts: 2
    • View Profile
"On Error" - Common Label within a SUB/FUNCTION...
« on: January 31, 2020, 12:57:03 pm »
Hello all,

I'm new to the QB64 world and am working to move an older quickbasic program to QB64 (As I imagine most here are...)
Anyway, the error that is impeding my ability to complete the conversion is "Common Label within a SUB/FUNCTION on XX Line" which is occurring on ALL  "ON ERROR GOTO X" statements. The error occurs with both text and numbered goto's.

I'm sure some more contextual information will be helpful - whatever you need, just ask.

Any help would be greatly appreciated.

Thanks,
Dan





Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: "On Error" - Common Label within a SUB/FUNCTION...
« Reply #1 on: January 31, 2020, 01:03:22 pm »
EDIT: Sorry, On Error requires a GOTO so forget what I had here.

Doesn't make sense to me, you should never use a GOTO to somewhere outside a sub. But neither do I use it, rather anticipate and head off errors before they happen.
« Last Edit: January 31, 2020, 02:20:37 pm by bplus »

FellippeHeitor

  • Guest
Re: "On Error" - Common Label within a SUB/FUNCTION...
« Reply #2 on: January 31, 2020, 02:03:13 pm »
On error goto label requires a label to exist in the main module, where error handling will happen. It seems you have multiple identical labels in multiples subs. You must concentrate error handling under one common label in the main module (before any subs).

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: "On Error" - Common Label within a SUB/FUNCTION...
« Reply #3 on: January 31, 2020, 02:21:18 pm »
Error trapping works with labels or numbers. As Fell just pointed out, the trap needs to be in the main module. If your's is not, meaning it or any number of them is in a sub, just cut and paste them into the main module.

Here is an example, just don't press ENTER when you get the "Press any key to continue prompt." I usually make my own key input routines, but I noticed I that ENTER will remain in the key buffer, even after the RUN statement is processed. I don't know if LINE INPUT responded the same way in QBasic, or not. It reminded me that QB64 may still need some debug work in regard to the RUN statement.

Anyway, here we go...

Code: QB64: [Select]
  1. PRINT "Select type of error statement to test..."
  2. PRINT "[1] Line number"
  3. PRINT "[2] Label"
  4. PRINT "[0] End routine..."
  5. LINE INPUT "Selection? "; x$
  6.  
  7.     CASE "0"
  8.         SYSTEM
  9.     CASE "1"
  10.         ON ERROR GOTO 100
  11.         CALL mysub1
  12.         END
  13.     CASE "2"
  14.         ON ERROR GOTO error_trap
  15.         CALL mysub2
  16.         END
  17.  
  18.     CASE ELSE
  19.         PRINT "Sorry, that was not an acceptable response. Take a time out. :D :D :D"
  20.         _DELAY 5
  21.         RUN
  22.  
  23. 100
  24. PRINT "Error:"; ERR; "Press any key to resume..."
  25.  
  26. error_trap:
  27. PRINT "Error:"; ERR; "Press any key to resume..."
  28.  
  29. SUB mysub1
  30.     LOCATE 10000, 10000 ' Not possibe!
  31.     PRINT: PRINT "Test with line number trap completed. One moment..."
  32.     _DELAY 3
  33.     RUN
  34.  
  35. SUB mysub2
  36.     LOCATE 10000, 10000 ' Not possibe!
  37.     PRINT: PRINT "Test with label completed. One moment..."
  38.     _DELAY 3
  39.     RUN
  40.  


If you need more help, just post the code.

Pete
« Last Edit: January 31, 2020, 03:58:10 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline lemonskunnk

  • Newbie
  • Posts: 2
    • View Profile
Re: "On Error" - Common Label within a SUB/FUNCTION...
« Reply #4 on: January 31, 2020, 04:07:48 pm »
Thanks for all the input. I was unaware that the error traps must all the concentrated in one Module - the program is coming from the original quick basic and it uses "ON LOCAL ERROR GOTO X" so there are 10's of error traps all throughout all modules and subs.

Anyway, I will make a concentrated error trap that handles all error cases for the whole module and let you know how it goes!

Thanks,
Dan

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: "On Error" - Common Label within a SUB/FUNCTION...
« Reply #5 on: January 31, 2020, 09:12:47 pm »
You can do it. I had 14 QB programs I combined into one QB64 program and I took all those individual error traps and combined them into one error trap. You may need to use a global variable to tell the error trap how to proceed, such as when a RESUME NEXT, RESUME [Line Number] or a RESUME 0 should be used. If you are not familiar with RESUME 0, it means go back to the line that threw the error. Anyway, good luck, and let us know how it turns out.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/