FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

DEBUGGING

Handle errors inside functions


I have quick rookie programming question reqarding error handling techniques:

How do you best handle errors in a loop when the loop may call a function many times? Calling an error checking FN in the loop can result in the user getting pasted with many many dialogs. Checking the error flag after the loop will not catch a single error in the middle that was later reset by a successful call.

Can a GOTO statement be used within a local fn? Warnings of dire consequences appear in several places reqarding the use of GOTO but no advice on whether they will work within a local fn.

Is there a variation on EXIT that will work? EXIT FN allows for no error handling and cleanup at the end of the function. I could not find any good examples in the manuals.

Can this be done without setting a global flag?

Skeleton Code:

LOCAL FN WorksMostOfTheTime(aWholeBunch%)
  FOR x% = 1 to aWholeBunch%
    err% = FN SomeTimesRunsOutOfMem%(x)
    IF err% THEN 'what?
'how do I get out of loop gracefully and down to some error handling before the end of the function?
  NEXT

'error handling
END FN

This seems like such a rookie question, but every solution I have come up with is awkward and/or slow.

Scott Goelzer


Q: is it a good idea to treat you error in the FN? (just for discussion)
The idea of FB is structure and reuse. I would have thought it more interesting to return an error code and have the error treated within your event loop. The same sort of error is liable to appear in different places, and like this you don't have to repeat that snippet all over the place.

How?
LOCAL FN WorksMostOfTheTime( aWholeBunch%)
  dim x%, err%
  dim myErrFlag%

  myErrFlag% = 0

  FOR x% = 1 to aWholeBunch%
    err% = FN SomeTimesRunsOutOfMem%( x%)
    LONG IF err%
      myErrFlag% = err%
      x% = aWholeBunch% +1 'to get out of loop
    END IF
  NEXT
END FN = myErrFlag%

* Then in your globals have a gErrFlag% that would be inited at zero at startup.
* Call boisterous FNs like this gErrFlag% = FN WorksMostOfTheTime( myVar%)
* Then in the event loop add a call to your error handler.

DO
  HANDELEVENTS
  IF gErrFlag% THEN FN whatsTheMishap
UNTIL blueInTheFace

IF gErrFlag% THEN FN sayWhyItCrashed
END

* In the error handler you can decide, probably with a SELECT/CASE the treatment for those errors. Minor ones can be dealt with Major ones will set blueIntheFace to _true.
* On leaving, FN sayWhyItCrashed will post an alert saying "Gotta Go 'cos something bizarre happened,
please allocate 4096 Mb to ^1
and try again" OK
or whatever, according to the cause of the error
(and where ^1 is the name of your app).

And, in this way you don't have to keep adding snippets, you can just add a new CASE in FN whatsTheMishap to cover each new misadventure. _And_ you can probably, in your next app, just dump FN whatsTheMishap straight in, and start dealing with errors from the beginning (and not as an afterthought at the end like we do really :-)

jonathan