TRAP

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: TRAP
Abbreviation: T Shift+R
Type: Command
Token code: 215/$D7
Handling routine
in BASIC ROM:
BASIC 7.0: 24397/$5F4D
List of all BASIC keywords


Remark: This article describes the BASIC command TRAP in Commodore BASIC V3.5 or higher.

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 ?UNDEF'D STATEMENT ERROR 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.[1]
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.[2] 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 ?TYPE MISMATCH ERROR appears. If out of range, the program stops with message ?ILLEGAL QUANTITY ERROR. A value greater than the highest possible line number 63999 always leads to the message ?UNDEF'D STATEMENT ERROR.

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 message ?UNDEF'D STATEMENT ERROR IN <line number> where <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:

  1. if the line number exists, RENUMBER changes it accordingly.
  2. if the line number does not exist the process stops with an ?UNRESOLVED REFERENCE ERROR message.

If RENUMBER encounter a TRAP statement with a parameter greater than 63999 (highest possible BASIC line number) a ?SYNTAX ERROR is raised.


Notes

  1. 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!
  2. 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]

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
[100][105][110][2000][2010] 11 SYNTAX ERROR IN LINE 110 !
[2020][110] ... CONTINUED
[120]
BASIC V3.5 Commands

ABS | ASC | ATN | AUTO | BACKUP | BOX | CHAR | CHR$ | CIRCLE | CLOSE | CLR | CMD | COLLECT | COLOR | CONT | COPY | COS | DATA | DEC | DEF | DELETE | DIM | DIRECTORY | DLOAD | DO | DRAW | DSAVE | END | ERR$ | EXP | FN | FOR | FRE | GET | GET# | GETKEY | GOSUB | GOTO | GRAPHIC | GSHAPE | HEADER | HELP | HEX$ | IF | INPUT | INPUT# | INSTR | INT | JOY | KEY | LEFT$ | LEN | LET | LIST | LOAD | LOCATE | LOG | MID$ | MONITOR | NEW | NEXT | ON | OPEN | PAINT | PEEK | POKE | POS | PRINT | PRINT USING | PRINT# | PUDEF | RCLR | RDOT | READ | REM | RENAME | RENUMBER | RESTORE | RESUME | RETURN | RGR | RIGHT$ | RLUM | RND | RUN | SAVE | SCALE | SCNCLR | SCRATCH | SGN | SIN | SOUND | SPC | SQR | SSHAPE | STOP | STR$ | SYS | TAB | TAN | TRAP | TROFF | TRON | USR | VAL | VERIFY | VOL | WAIT