To Err is Forbidden Part II

John D. Adams 13126 Tripoli Ave. Sylmar, CA 91342

When the TRS-80 discovers an error, it shifts to either command or edit mode. This seems perfectly sensible: If the machine cannot do what you have asked it to, or, if continued execution will produce nothing but garbage, the best thing is to stop. Stopping execution poses no problem unless you have non-program data stored in memory. In this case, there is no way to restart without losing the stored data.

Level II BASIC incorporates a short set of statements that set up an error trapping routine. These routines deal with errors in two ways. One handles loss of data. Another allows the computer to ignore an error of little consequence that occurs in a program so that execution can continue. For example, if I were taking logarithms from numbers generated internally and there was a chance a negative number could pop up, I might want to simply skip that number and go on.

Record Student Grades

I use a homemade program to record my students' grades. During the course of the program, individual test grades are used as divisors. When a student is absent for a test, a zero is entered until he takes a makeup, at which time the program changes the grade. Since zero cannot be used as a divisor, a potential exists for a division by zero error. As the program is tight on RAM space, I did not want to add another module to deal with this eventuality.

The error trapping routine below was developed to handle this problem. Its structure illustrates how such routines work.

10 ON ERROR GOTO 30000 30000 IF ERR/2 + 1 = 11 THEN RESUME NEXT 30010 PRINT-ERROR NUMBER ";ERR/2 + 1;"IN LINE "; ERL


The ON ERROR GOTO instruction in line 10 initiates the routine, substantially altering the way in which errors are processed. When an error is discovered, execution doesn't shift to command or edit mode, but proceeds to the indicated line number for further instructions. This process works much like a subroutine, and the instruction, like GOSUB, works as a paired instruction. When the routine has finished, a RESUME is necessary. Obviously, this instruction must be read before an error occurs and should be piaced at the beginning of the program.

The routine could be terminated at this point by adding the line: 30000 RESUME NEXT. This would cause the computer to ignore the error and branch back to the next program statement to continue execution. In my grade program, this is what I wanted if the error was an attempt at division by zero. But, if it was some other error that materially altered program output, that error would also be ignored.

The 80 reports errors using abbreviations; these codes appear on pages BI2 and B/3 of the manual. Computers deal with everything as numbers, however, and their conversion into letters is for human convenience. The error condition is carried internally as a number, which can be used in error trapping to designate specific errors. The instruction ERR/2 -f 1 in lines 30000 and 30010 is for this purpose. Entering PRINT ERR/2 +1 after an error occurs will return a number which identifies the error. The number codes for errors appear on page B/1 of the manual.

In my grade program, the error I want ignored is division by zero, error code 11. Line 30000 means, "If the error you have found is division by zero, forget it and pick up execution on the line or statement following the error. If it is not, then drop to line 30010."

PRINT ERL returns the line number in which the error happened. Line 30010 tells us what and where the error is, and line 30020 stops execution so we can make some decisions. Possibly the error involves inoperable data, such as trying to take the square root of a negative number. In this case, we just want to go back to make another data entry. On the other hand, if the error is one that needs fixing, we are going to have to leave the execute mode for repairs.

Dumping Data

After adding, deleting or editing fines, we cannot continue execution and running the program will cause data loss. If there is considerable data involved, a module can be built into the error trapping routine itself to dump data to tape before you hit the break key. This makes a matching loading routine necessary.

There are defaults for the ERL instruction: If no error has been made, it returns a zero; if the error was made in direct mode, such as trying to use INPUT in command mode, the number 65535 will be returned.

The RESUME statement is similar to RETURN; its action is similar, but options are available which enhance its effect. Used alone, as RESUME 0, it will return to the statement in which the error was commit ted. Run these lines:

Since we cannot take square roots of nega-

Condominium Hotel Reservation Accounting System (MOO II)

50-100 units and reservations tor 14 months on 2 drives lull reservation display automatic unit selection accounting on a jnit Dasis monthly account ng reports lor owners

(Dealers wantedi $1500 U S.

Application Development Code tor MOD (TRSDOS)

Set ot gosubs lor lull screen l O 'lie record field definition record read wr,te. unpack pack change delete update add tile record print date conversion error handling Reduces any application to gosub call and logic

Was this article helpful?

0 0

Post a comment