ARCHIVE SERIES
PART 22 : ERROR HANDLING IN ARCHIVE WITH ERRNUM() FUNCTION and ERROR COMMAND
I write commercial software in the ARCHIVE programming language. Much of our computer usage involves storing and retrieving information and a database is a good place to store such information. A nice structured database language as found in ARCHIVE makes it a natural place to write programs to handle information. But one requirement of any language that is to be used to write well behaved programs is a way to handle errors. Such things as detecting when a device can’t be written to or when a file can’t be located. ARCHIVE has the necessary tools for dealing with such errors so the program can take the appropriate action or at least warn the user about the error. Below I will give some examples of how errors work including several little error procedures that you can include in your ARCHIVE programs to make them better able to recover from errors. In order to work through the examples start up your QL and start ARCHIVE. A couple of the examples will put ARCHIVE in loops that can’t be broken out of so be prepared to reboot.
UNTRAPPED ERRORS IN ARCHIVE
Normally when an error happens in ARCHIVE you are given an error message number and a description. The number can be looked up in the error section of the ARCHIVE chapter of your QL manual for more detail. If the error occurs within a procedure you will also see the procedure name and the statement within the procedure where the error occurs. This works very well while developing a programs as the error messages are generally pretty clear and since you see the statement where the error occurs you can usually immediately find the error, start the editor, make the correction, and be on your way. This is much easier and faster for program development than with a compiled language which requires recompiling and linking and the error statements are often cryptic.
Try these errors :
new<ENTER> clear everything 1<ENTER> "1" is not a command print a<ENTER> "a" has not been defined print ((2*3)/5<ENTER> incomplete parenthesis
Now try an error in a procedure
edit<ENTER> start the editor proc1<ENTER> first procedure proc2<ENTER> call second procedure <ESC> exit proc1 edit <F3>n<ENTER> create new procedure proc2<ENTER> call it proc2 print "hello"<ENTER> okay statement print world<ENTER> will cause error <ESC> exit proc2 edit <ESC> exit editor
Now start procedure at the ARCHIVE prompt.
proc1<ENTER>
Notice that “hello” was printed from procedure proc2 but when the second print statement was reached the error caused the program to halt and you see the offending procedure name and the statement in it where the error occurred plus the error description and number. You can immediately start the editor and correct the statement.
edit start the editor
<TAB> move to proc2 procedure
2<DOWN ARROW> move to statement with error
<F5> edit statement
{insert " around 2 so it reads print "2"}
<ENTER> accept correction
<ESC> leave editor
proc1<ENTER> see it is okay now
This is all well and good for program development but sometimes you want to catch the errors before they cause the program to halt and deal with them within the program. An example would be if you prompted for a database name. If you mistype the name it would be better to catch the error within the program and reprompt rather than have the program halt and then have to restart it. In fact in a commercial program you would want to trap every error so the user would never be confronted with a halted program. How to do this will be covered below.
THE <ESC> KEY
First we should discuss the <ESC> key as it is used by ARCHIVE. When pressed it is usually interpreted as a special error which will halt any running ARCHIVE program. It is simply a way to stop a program. As mentioned each error in ARCHIVE has a number. <ESC> is called Error 27 which also happens to be the same as the ASCII code for the <ESC> key. This means that most of the time when you are running an ARCHIVE program and press the <ESC> key you will cause the program to halt due to Error 27.
At the ARCHIVE prompt try pressing <ESC> and you will see “<ESC>” displayed on the screen. For an example of <ESC> not causing an error try this at the ARCHIVE prompt :
print code(getkey())<ENTER> set GETKEY for input 1 display 1 ASCII code <F5><ENTER>$ display $ ASCII code <F5><ENTER><ESC> display <ESC> code <F5><ENTER>a display a ASCII code
This first line is a command line program which uses the GETKEY() function (see description under functions in manual) to wait for a key to be pressed and then will display the ASCII code of the key pressed. The second line inputs the “1” character to GETKEY and you see the ASCII code of “1” which is 49 appear in the upper screen. The next line recalls your last line which is the getkey function you just entered then you enter it and press “$” and you see the ASCII code for “$” namely 36 appear on the screen. The next line does the same only your press <ESC> and instead of causing an error it is accepted and the ASCII code for <ESC> namely 27 is displayed on the screen. The last line does the same for “a”. So you see that the GETKEY() function will not recognize <ESC> as an error but merely treats it as a key press like any other.
To show how this might be used in a procedure start the editor and enter this procedure which is a loop. You may have to reset your QL to halt the loop.
new<ENTER> clear everything edit<ENTER> start the editor loop<ENTER> procedure name while 1<ENTER> start of endless loop print code(getkey())<ENTER> getkey function endwhile<ENTER> end loop <ESC> exit procedure <ESC> exit editor
Now start the procedure at the ARCHIVE prompt.
loop<ENTER> start procedure
Try pressing any key and you will see the ASCII code on the screen. The <ESC> key will not halt the procedure. Pressing it as fast as possible won’t do it either but if I hold the key down so it auto-repeats it may be fast enough to halt the program. Presumably this causes the <ESC> to happen fast enough to occur during the print part of the procedure and so can be intercepted by ARCHIVE and the program is halted. I am using a Gold Card but a SuperGold Card may be so fast that the loop can’t be broken.
Let us try another type of loop which under certain circumstances can’t be halted by <ESC> :
new<ENTER> clear out any procedures edit<ENTER> start editor loop2<ENTER> new procedure name let i=1<ENTER> counting variable while 1<ENTER> start of endless loop print i<ENTER> print loop counter let i=i+1<ENTER> increment counter endwhile<ENTER> loop end <ESC> exit procedure <ESC> exit editor
Now start the loop and let it go a while then halt it with <ESC>.
loop2<ENTER> start procedure <ESC> halt it after a while
Now try this. You will find it can’t be stopped.
loop2<ENTER> start procedure <SPACE BAR> a non<ESC> character <ESC> try to abort
I think you will find there is no way to to break this loop. This shows an oddity of ARCHIVE. Pressing any character other than <ESC> on a running procedure can block ARCHIVE from recognizing <ESC> at a later time. ARCHIVE must only look at the first character of the waiting input buffer which is not <ESC>.
Other times when <ESC> does not immediately work are when ARCHIVE is doing an internal task that takes a long time such as ordering or selecting on a big file. When the task is done it will then halt but not immediately. As you can see <ESC> is a mixed blessing. It is nice to be able to halt a program but is doesn’t work 100% of the time and if the <ESC> is hit by mistake it can halt a program when you don’t want it to.
ERRNUM() FUNCTION
ARCHIVE has a function which returns the number of the last error that has occurred. Like all ARCHIVE functions it has parenthesis to indicate it is a system function but like many ARCHIVE functions there is nothing needed within the parenthesis since no arguments are needed for this function. ERRNUM() just returns an integer number which can be looked up in the error list in the manual. If a zero is returned then no error has yet occurred. Once an error has occurred then ERRNUM() continues to return that error number until another error occurs.
Let us see what how it works.
print errnum()<ENTER> see current value errnum()<ENTER> an error (1) print errnum()<ENTER> see error now 1 <ESC> <ESC> error 27 print errnum()<ENTER> error now 27 new<ENTER> clear everything print errnum()<ENTER> last error not cleared
Why was the 2nd statement above an error? Well errnum() just returns a number so it would be the same as typing something like :
5<ENTER> error
You will notice that once an error has occurred then errnum() will continue to return the value of that error until a new error has occurred even if you clear out any procedures and variables with the NEW command. How do you reset the value to zero (no error)? See below.
ERROR PREFIX COMMAND
ARCHIVE has an error command which is used in front of a procedure name when you call a procedure. It will cause you to return to the statement right after the ERROR statement if an error occurs within the procedure instead of halting the program. For example if you want to have a procedure to prompt for a database name and then LOOK at the database then you can use the ERROR statement as a prefix when you call the procedure and then right after the call check for an error with ERRNUM(). If no error occurred then you can assume the LOOK worked and you can go on but if an error occurred then deal with it in the program. When you use ERROR on a procedure and no error occurred then ERRNUM() gets reset to 0. Let us set up an example and make sure to have a copy of gazet_dbf on flp1_ .
new<ENTER> clear everything edit<ENTER> start the editor main<ENTER> name for first procedure print "MAIN"<ENTER> print statement error looker<ENTER> use error on looker procedure if errnum()<ENTER> check for error print "Unable to access flp1_";f$<ENTER> print "Error = ";errnum()<ENTER> main : stop<ENTER> redo procedure endif<ENTER> end of error checking print count()<ENTER> print record count close<ENTER> close <ESC> exit main <F3>n make new procedure looker<ENTER> procedure name input "File to look at : ";f$<ENTER> look "flp1_"+f$<ENTER> <ESC> exit editing looker <ESC> exit editor
Now let us see how it works.
main<ENTER> start program xyz<ENTER> bad file name
An error should have occurred when the looker procedure tried to look at file “xyz”. You are given the error messages and asked to reenter the file name.
gazet_dbf<ENTER> good file name
Did everything work as expected or did you have any unexpected errors. If you had a typo error in looker you would not get an detailed error message about it. The ERROR command blocks normal error messages and make debugging difficult. To see this let us put an intentional bug in looker.
edit<ENTER> Start the editor 2 <DOWN ARROWS> go to last statement in looker <F4> insert mode for looker bug<ENTER> error line <ESC> get out of insert mode <ESC> get out of editor main<ENTER> start program gazet_dbf<ENTER> good file name
Notice we still have an error because of the bad line we put in looker but we can’t tell what it is because our ERROR prefix to looker in main keeps the program from crashing. We can see the number of the error in our message in main but we can’t see the offending statement. Let us remove the ERROR prefix from the looker call so we can see our “bug”.
close file still accessed
edit<ENTER> start editor
<TAB> move to main procedure
2 <DOWN ARROW> move to error looker
<F5> edit call to looker
{remove "error" so line just reads looker}
<ENTER> accept corrected line
<ESC> exit editor
main<ENTER> start program
gazet_dbf<ENTER> enter database name
Now we see the offending line with our “bug” in it. You may find when you are designing a program and are trapping errors you will have to remove some ERROR prefixes until you get your own bugs out of the program. Let us remove the “bug”.
close : edit <ENTER> close file and start editor 3<DOWN ARROWS> move to bug <F3>c<ENTER> cut line
SOME HANDY ERROR PROCEDURES
I have developed several useful procedures using the error features of ARCHIVE and will list and describe them now.
FILE CLOSING
When an ARCHIVE program is first started you can’t be sure if any files are already open from any previous work. To be completely safe it is best to close any open or looked at files. How can this be done when you don’t even know what their logical name may be? In ARCHIVE if 2 files are open then one of them is always active. If you use the CLOSE command then the active one will be closed and the other will become active then you can close that one. This works even if you don’t know the file name or logical name. So to make sure all files are closed simply make a loop to close files and it will keep closing files until none are left open and then an error will occur. But if you used the ERROR prefix when you called the closing procedure then the error will not halt your program and you can go on an confidently assume all files are closed.
proc close_all
while 1 : close : endwhile
endproc
To use it simply put the line :
error close_all
in your program. Be aware that always after you call close_all that errnum() will not be 0 since an error has occurred when close_all tried to close a file when none was opened.
CLEARING ERRNUM()
As mentioned above once an error has occurred the value for errnum() becomes nonzero. Sometimes we need to set it to zero so we can later check it to see if any errors have occurred. For instance we could have 2 levels of error checking. If the lower level finds and error and corrects it then we want to reset errnum() so the upper level won’t think there was an error it should deal with because errnum() is nonzero. To do this simply call a procedure with the ERROR prefix which does nothing so no error occurs and this will make errnum() be zero. I create a do nothing procedure to accomplish this.
proc cleary
endproc
Now when you have the following statement in you program :
error cleary
you will set errnum() to zero and it will stay that way unless an error occurs later on.
GIVING A GENERAL ERROR MESSAGE
In a complex program it is nice to have a standard way to notify a user of errors. Part of that would include an error procedure which gives a message and gives the error number so the user can look it up. Of course it would be possible to even keep a special database of error numbers and descriptions so the correct message could be given for each error but we will use a simpler example here.
proc errmess;m$
if errnum()
print at 23,0;tab 80;at 23,1;
print "Error ";errnum();" during ";m$;
input ".Press <ENTER> : ";i$
endif
endproc
Notice several things about this procedure. It clears line 23 and then puts the error message there. This means you must be working in mode 0 or line 23 is out of range. The error message line consists of the error number plus a message passed to it by the calling procedure. The message must be short so everything will fit on the line. The calling procedure must take any appropriate action as this message procedure is dumb. It only gives an error message that the user must acknowledge but then returns to the calling procedure.
To illustrate the use of these procedures modify “main” to be as below, add the above error procedures, and “looker” is unchanged. All needed procedures must be present.
proc main
mode 0 : error close_all : error cleary
print "MAIN"
error looker
if errnum()
errmess;"access of flp1_"+f$
main : stop
endif
print f$;" has ";count();" records"
endproc
You may have to remove some of the error prefixes while you debug the program. Notice that the passed message was concatenated (+) together while the printed line can use appending (;). Passed text values must always be one long concatenated string in ARCHIVE.
ERROR MEANINGS
As you use ARCHIVE and make errors you will learn to recognize them quickly. Misspelling a procedure or variable often gives error 1 – Command not recognized or error 8 – Variable not found. Having mismatched parenthsis gives error 70 – evaluator syntax error. Calling a procedure and leaving off needed arguments gives error 2 – end of statement expected at the called procedure not where it was called so it is tricky to locate if there are many calls to it. Error 21 – File already exists has been left out of the manual.
Until next time may your errors be easily found and happy ARCHIVING!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.