XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

throw


Type:   -   special form (fsubr)
Source:   -   xlcont.c, xljump.c

Syntax

(throw tag-symbol [expr])
tag-symbol - an expression that evaluates to a symbol
expr - an optional expression to be returned
returns - never returns

Description

The catch and 'throw' special forms allow for non-local exits and traps without going through the intermediate evaluations and function returns. The 'expr' in 'throw' specifies what value is to be returned by the corresponding catch. If there is no 'expr', a NIL is returned to the corresponding catch. If a 'throw' is evaluated with no corresponding catch , an error is generated:

error: no target for THROW

If, in the calling process, more than one catch is set up for the same 'tag-symbol', the most recently evaluated 'tag-symbol' will be the one that does the actual catching.

Examples

(catch 'mytag)                          ; returns NIL - no THROW
(catch 'mytag (+ 1 (+ 2 3)))            ; returns 6   - no THROW
(catch 'mytag (+ 1 (throw 'mytag)))     ; returns NIL - caught it
(catch 'mytag (+ 1 (throw 'mytag 55)))  ; returns 55  - caught it
(catch 'mytag (throw 'foo))             ; error: no target for THROW

(defun in (x)                           ; define IN
   (if (numberp x) (+ x x)              ;   if number THEN double
                   (throw 'math 42)))   ;             ELSE throw 42

(defun out (x)                          ; define OUT
  (princ "<") (princ  (* (in x) 2))     ;   double via multiply
  (princ ">"))

(defun main (x)                         ; define MAIN
  (catch 'math (out x)))                ;   with CATCH

(in 5)                                  ; returns 10
(out 5)                                 ; prints <20>  returns ">"
(main 5)                                ; prints <20>  returns ">"
(main 'a)                               ; prints <     returns 42

Note: Although catch and 'throw' will accept a 'tag-symbol' that is not a symbol, it will not find this improper 'tag-symbol'. An error will be generated:

error: no target for THROW

See the throw function in the XLISP 2.0 manual.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next