BASIC error handling
Julie Stamp (8365) 474 posts |
Can we pretty please have improved local error handling, along the lines discussed in this thread? A simple case would be rethrowing after tidying up in a procedure,
|
Rick Murray (539) 13840 posts |
Uhhh…? Don’t you want an FN instead of a PROC there? In that way, it can return zero if the operation worked, and anything non-zero is some sort of error code. Therefore, ERROR needs only be local use for trapping BASIC statements that may raise errors. Otherwise, the function passes back values to say what happened, or if you only care whether or not it worked, a simple TRUE/FALSE. So: [...blah...] IF ( FNdo_file("$.thingummajig" <> FALSE ) THEN [...oh crap...] ENDIF [...] DEFFNdo_file(f$) LOCAL H% LOCAL ERROR H% = OPENIN f$ IF (H% = 0) THEN : =1 ON ERROR LOCAL RESTORE ERROR : IF H% <> 0 CLOSE#H% : =2 [... process file ...] CLOSE#H% =FALSE |
Julie Stamp (8365) 474 posts |
In my actual code it is a function, trapping BASIC statements is the issue, specifically passing both ERL and REPORT$ up to the error handler of the caller. |
Rick Murray (539) 13840 posts |
It doesn’t have to be raised as an error does it? Could the function not return zero or construct an error block? Think of how C does it – because there’s pretty much no centralised error handling in C except for the things that signal() provides, and if you hit one of those, the best thing you can usually do is tidy up as best you can and then abort. [I know you know this, just explaining it here for people who are unfamiliar with C] |
Stuart Swales (1481) 351 posts |
Don’t think that you can propagate ERL. I usually have DEF PROCraise IF NOT raised% THEN raised%=TRUE: ERROR ERR,REPORT$+" at line "+STR$ERL+" in ThisProg" ERROR ERR,REPORT$ ENDPROC ... DEF FNdo_stuff LOCAL H% LOCAL ERROR: ON ERROR LOCAL: RESTORE ERROR: CLOSE#H%: PROCraise ... RESTORE ERROR CLOSE#H% ENDPROC |
Martin Avison (27) 1494 posts |
Can you not simply use something like: |
Alan Adams (2486) 1149 posts |
My great annoyance with BASIC error handling is that the stack is deleted before the handler takes effect. This means that *reportstack in an error handler doesn’t give you any information about the route to the error. This usually means I have to add *reportstack to the lowest function/proc I can identify, to be called every time the function is entered, and then trigger the error again – if I can work out how to trigger it. |
Stuart Swales (1481) 351 posts |
@Martin If you have more than one nested local handler you just get a string of appendages that may unhelpfully grow too long I think if ERL is important to the caller’s hander you could use DEF PROCraise IF NOT g_erl% THEN g_erl%=ERL ERROR ERR,REPORT$ ENDPROC |
Martin Avison (27) 1494 posts |
@Alan: Yes, that annoys me as well. The only solution I have is a variation on the above: but it still needs doing in each low-level proc you are investigating.
|
Martin Avison (27) 1494 posts |
@Stuart: Yes – if you have several nested handlers using a variable like that would be better. |
Steve Drain (222) 1620 posts |
That was some thread, wasn’t it? ;-)
What that needs is DEFFN_ERROR LOCAL k% DIM k% LOCAL TRUE k%!-8=!&8684 =TRUE which you call as There was also a method for nested error handling using Would you like your FN/PROC routines to report errors including where they are called from? A bit more magic can do that. It might even be easier than reporting the stack. ;-) |
Steve Drain (222) 1620 posts |
It might be worth posting the assembler for a DEFPROCCALL_ERROR DIM P% 11 [OPT 2 ._ERROR LDR r0,[r8,#-124];pointer to error handler STR r0,[r13,#16];return address from CALL MOV pc,lr ] ENDPROC |
jgharston (7770) 14 posts |
IF H% = 0 ERROR 0, “Coudln’t open file” Two problems with that. Error 0 is a fatal error, defined to stop the interpreter and bomb out to the command prompt. The error number for “couldn’t read a file” is 214. Not some random number made up by yourself, but 214. (192 for “couldn’t write to a file”.) IN% =OPENIN :IF IN%=0 THEN ERROR 214,“Couldn’t open file” OUT%=OPENOUT:IF OUT%=0 THEN ERROR 192,“Couldn’t save to file” If you generate errors yourself you must must must MUST MUST use the correct error numbers, otherwise you are kicking in the nads anything that uses ERR – which includes other programs that call your program, not just yourself. You can’t just say “I don’t care what my error numbers are” – the outside world cares. |