|
Post by rikky on Nov 5, 2020 17:49:47 GMT 1
Hello, I have this program: PRINT (LOAD$("test.txt")) It gives: Runtime error: function 'LOAD$/BLOAD' at line 1 in 'test.bac': Error opening file: No such file or directory echo $? gives : 0 Also in BaCon END RETVAL gives : 0 LOAD$ doesn't give a RETVAL that can be catched by ba(sh) This isn't rely important of course, but it would be nice. It can be worked around easily: IF FILEEXISTS("test.txt") THEN EPRINT "Error opening file: " & BASENAME$(TOKEN$(ARGUMENT$,1)) & " file doesn't exist" END 1 ENDIF Rik. EDIT: but then the other way around : IF NOT(FILEEXISTS("
|
|
|
Post by Pjot on Nov 5, 2020 21:01:33 GMT 1
Hi rikky,
A program should return the actual ERROR value to the shell when an error occurs. This didn't work for all circumstances and is now fixed (latest in fossil).
Apart from this, note that you can ignore errors altogether by setting "OPTION ERROR FALSE" in the beginning of your program. The program will proceed as if nothing has happened.
Also you can set your own error callback which allows a self-defined exit code (0-255). For example:
OPTION ERROR FALSE CATCH ERROR help
SUB help(c$, f$, no) PRINT "Error is: ", ERR$(ERROR), " in function ", c$, " in file '", f$, "' at line ", no END 123 END SUB
a$ = LOAD$("not-existing-file.txt")
In this example, the BASH "$?" command returns 123.
HTH Peter
|
|
|
Post by rikky on Nov 6, 2020 7:19:19 GMT 1
I see, I never understood this thing, for I could not grasp where the c$ f$ and no were coming from. But I guess they are generated by the CATCH statement. Aha. Thanks. Rik.
|
|
|
Post by bigbass on Nov 12, 2020 20:19:00 GMT 1
Hello Rik
you wanted a feature RETVAL for LOAD$
we can make a homemade MACRO to simulate that idea (sort of)
but once again this type of thing is not even close to official bacon code it could be useful when you need to extend a built in command or function
we also get the low level error reported correctly
ALIAS perror TO FALT ALIAS fclose TO ECLOSE DECLARE myfile TYPE FILE*
'---ELOAD means error test on load '---ELOAD with a return value test defaults to read only "r" DEF FN ELOAD(a) = fopen(a,"r")
myfile = ELOAD("nada.bac") IF myfile == NULL THEN FALT("You got this error ") END 1 ELSE WHILE NOT(ENDFILE(myfile)) READLN txt$ FROM myfile PRINT txt$ WEND ECLOSE(myfile) END IF
|
|
|
Post by rikky on Nov 14, 2020 9:26:45 GMT 1
Thanks Bigbass, For now however I tend to go wit the Pjotr solution. For it's the most simple one to remember. OPTION ERROR FALSE
CATCH ERROR help
SUB help(function_name$, filename$, lineno) PRINT ERR$(ERROR), " in function ", function_name$, " in file '", filename$, "' at line ", lineno END 123 END SUB
PRINT LOAD$("nops") Rik.
|
|