in BASIC ROM:
|BASIC 7.0: 24397/$5F4D|
|List of all BASIC keywords|
Type: Command General Programming-Syntax: TRAP [line number]
The TRAP is used to detect and correct programming errors while a program is running.
When turned on, TRAP intercepts most error conditions (excluding DOS error messages but including the STOP key) except an in caused by TRAP itself. In the event of any execution error, the error flag is set and the execution is transfered to the line number specified in the TRAP statement.
Any error in the trap routine itself cannot be caught.
The line number in which the error occurred can be found by using the system variable EL. The specific error condition is kept in the system variable ER. The string function ERR$(ER) gives the error message corresponding to any error condition.
The RESUME statement can be used to resume program execution. TRAP with no line number turns off error trapping. An error in a TRAP routine can not be catched. See also system variables ST, DS, and DS$.
The parameter <line number> (in contrast to GOTO and GOSUB) might consist of an arithmetic expression giving a value which has to be in range from 0 to 65535. If the result is not numeric, the error appears. If out of range, the program stops with message . A value greater than the highest possible line number 63999 always leads to the message .
TRAP does not check if the line with the catch-up routine really exists. In case this line is not found the interpreter emits a rather irritating error messagewhere <line number> points to line which raised the error, not the line with the errornous TRAP statement.
The BASIC statement RENUMBER is also covering the TRAP statements, but this works only if the line number parameter is a numeric constant (not a general arithmetic expression). In case a numeric value is given, two cases are possible:
- if the line number exists, RENUMBER changes it accordingly.
- if the line number does not exist the process stops with an message.
If RENUMBER encounter a TRAP statement with a parameter greater than 63999 (highest possible BASIC line number) ais raised.
- Errors in the trap routine itself is not catchable: Except in BASIC 7.0, if RESUME with a line number and the addressed line does not exist!
- Just after leaving the catch-up routine with RESUME the system variables ER and EL are set back to -1 or 65535, respectively.
Example[edit | edit source]
100 TRAP 2000 105 FOR I=1 TO 2000: NEXT : REM WAIT APPROX 2 SECS 110 FATAL SYNTAX ERROR : PRINT " ... CONTINUED" 120 END 2000 REM TRAP CATCH-UP ROUTINE 2010 PRINT ER; ERR$(ER) " ERROR IN LINE" EL "!" 2020 RESUME NEXT
This program's output should be
11 SYNTAX ERROR IN LINE 110 ! ... CONTINUED
In case someone stops the program within the first 2 seconds with key RUN/STOP to output would look like
30 BREAK ERROR IN LINE 105 ! 11 SYNTAX ERROR IN LINE 110 ! ... CONTINUED
In this variation catches all occurring errors, but this can lead to other problems. It is recommended to make the routine flexible in regard to the error code.
The program trace can be better watched with active tracing (TRON), especially the branching to the trap routine starting at line 2000:
TRON RUN  11 SYNTAX ERROR IN LINE 110 !  ... CONTINUED