Good morning everyone.
As I mentioned in the last episode, I will talk about error handling, how to work with function keys and control the STOP key.
The mistake that is often made is the so-called typing error, we will see the classic SYNTAX ERROR appear on the screen. Fortunately our MSX brings us the line where it happened and we can check it to make corrections. The problem is when more serious errors occur, i.e. an error due to an incorrect function or data set. To give a trivial example, when a matrix goes below zero, because in our calculations we have not put a check for this to happen. In this case, the MSX reported the error on that line, but actually the error was committed first. This is to make you understand that we will not always solve our problems with error management. In order to start the routine we will put at the beginning of the program: ON ERROR GOTO Line number. When any error occurs, execution will be sent to the line indicated in the command. At the end of the routine we use the RESUME function to return the execution to the point where it was interrupted. There are 3 different ways to use RESUME; RESUME Line number, will send the program to the specified line. RESUME NEXT, which will move it to the next line where the error occurred and finally RESUME [0] which will re-execute the line where the error was made. We can also know in which line the error occurred and its code with the ERL and ERR functions. ERL tells us the line and ERR the code (You can see the error codes in any manual or book that talks about MSX Basic). There is also the possibility of simulating an error with the ERROR Error Code function. As an error code we will take an error not included in the table, ranging from 72 to 255. To deactivate error interception, we will write: ON ERROR GOTO 0. Below I insert a small program to make it better understood.
10 ON ERROR GOTO 1000
……..
100 INPUT “Enter the file name”; N$ : OPEN N$ FOR INPUT AS #1
…….
500 IF LEN(f$) > 30 THEN ERROR 100
…………
1000 IF ERR=66 THEN PRINT “Disk full. Please change disk and press space to continue”: GOTO 1040
1010 IF ERR=53 THEN PRINT” The file does not exist. Please enter the correct name”: RESUME
1020 IF ERR=100 THEN PRINT “Sentence too long”: RESUME NEXT
1030 PRINT “Error No.; ERR; “to the line”; ERL; “program finished”: ON ERROR GOTO 0: END
1040 IF INKEY$<>CHR$(32) THEN 1030 ELSE RESUME 0
I assume that our program manages many files. In the first line of the error interception subroutine, i.e. line 1000, when the disk is full it will give us the possibility to change it by repeating the operation. RESUME or RESUME 0 are the same thing. On Line 1010, it intercepts the error on line 100 or similar and gives us the option to re-enter the file name. Line 1020 will be processed with the error code 100 created on line 500, upon the occurrence of a sentence that is longer than expected. The program returns to the next line (RESUME NEXT), but I could easily send it back to the insertion line: RESUME Line Number. The last line, 1030, comes into execution only when it has encountered an error not foreseen by the program, at this point it tells us where it occurred and its code, deactivates the routine and exits.
The KEY command can be used to disable the display of the line at the bottom of the screen by typing KEY OFF. If we want to make it reappear we will write KEY ON, if instead we want to display the entire list on the screen we will write KEY LIST. The 10 function keys are programmable, to change the description of one of the keys we will write: KEY Key Number, “Text”. The key number ranges from 1 to 10, the text can be a maximum of 15 characters long, but in the bottom line of the screen we will only see 7 characters per key, due to space problems. We can also use special characters. Example: KEY 1,”PRINT” + CHR$(34) + “HELLO” + CHR$(34) + CHR$(13) . To activate function key interception we will use the command ON KEY GOSUB Line Number, Line Number, etc. We can enter up to 10 line numbers as they are function keys. We can also skip one by leaving a blank space. For example, we want to use the F1, F2 and F4 keys, our line will be written like this: ON KEY GOSUB 1000,2000,,3000. Like all the interrupts that I explained previously, to activate them they need another command: KEY (Key Number) ON, if we want to deactivate them we will put OFF or STOP to suspend them.
100 ON KEY GOSUB 1000,2000,,3000: KEY(1)ON: KEY(2)ON: KEY(4)ON
If we want a program not to be blocked with the STOP key, at the beginning we will insert the following line: ON STOP GOSUB Line Number and immediately after we will activate it, adding STOP ON. With STOP OFF we deactivate it, while with STOP STOP we suspend it. In the subroutine we can insert a sentence or just the RETURN function. The STOP command can also be used to check programs. To give an example, we want to see what happens at a certain point of the above, we will insert the command at that point to stop it so we can do our checks. There is the possibility to start the program from the interrupted point using the CONT command. We can use the latter even if the interruption was made with the STOP button, but to use it there must be the right conditions, otherwise the MSX will signal an error message: “Can't continue”.
This episode has also come to an end. Thanks again to all the people who are following this long column. See you next time 🙂


0 Comments