Author Topic: SUB error handlers  (Read 2315 times)

0 Members and 1 Guest are viewing this topic.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
SUB error handlers
« on: January 15, 2021, 08:22:38 am »
As We All Know(tm), ON ERROR can only go to a handler in the main program, not in a subroutine. However, the QB45 manual page for ON ERROR has this at the bottom:
Quote
SUB and FUNCTION procedures and DEF FN functions can contain their own
error handlers. The error handler must be located after the last
executable statement but before the END SUB, END FUNCTION, or END DEF
statement. To keep the error handler from executing when there is no
error, the procedure or function must terminate with an EXIT SUB, EXIT
FUNCTION, or EXIT DEF statement immediately ahead of the error
handler, as in the following example:

SUB InitializeMatrix (var1, var2, var3, var4)
      .
      .
      .
   ON ERR GOTO ErrorHandler
      .
      .
      .
   EXIT SUB

   ErrorHandler:
      .
      .
      .
   RETURN

END SUB
Which seems to suggest you can, but I couldn't get it to do anything in QB45.

The really strage part is that it's ON ERR, not ON ERROR, which seems to be more akin to the general ON ... GOTO ... which would take a list of line numbers and branch depending on the value of an expression. That at least explains why the example is syntactically correct. And it's terminated with RETURN, which is usually paired with GOSUB (I would expect to see RESUME in an error handler).

So were the manual writers at Microsoft smoking something when they did this page?

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: SUB error handlers
« Reply #1 on: January 15, 2021, 02:46:32 pm »
ERR is a reserved QB error code variable. ERR = 9 is subscript out of range. What that code you posted from a manual looks like is, well, possibly part of a larger snippet, not included, which would have a RESUME statement, after the error is handled in the main (and of course that code not shown would also have to make that error handler in the main) Then, as you say, I agree, that ON ERR GOTO is exactly like ON GOTO or the ON GOSUB statements.

I cannot recall ever being able to place an error handler in a sub or function. You could put them in the main of each "module" in a multi-modular program.

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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: SUB error handlers
« Reply #2 on: January 15, 2021, 03:20:45 pm »
maybe they were fasting, low level of glucose into the blood gives hallucinations

@ Pete
about error managment... ON GOSUB and ON GOTO are an ancient way to make a separated block of code while you are sharing all the variables... a middle way between Spaghetti code and Sushi code. 
Why isn't the error managment able to use a better closed code block like SUB/FUNCTION? In QB45 it was so, but in the development of QB why it stays there? Only for backward compatibility?

PS boys you rock!
Programming isn't difficult, only it's  consuming time and coffee