Structural Informatics Group (SIG) logo
Home | Projects | Demos | Downloads | Publications | Local Info | About Us | New site
Go to the first, previous, next, last section, table of contents.



type: special form (fsubr)
location: built-in
source file: xlcont.c and xljump.c
Common LISP compatible: yes
supported on: all machines


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


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.


(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".

Go to the first, previous, next, last section, table of contents.