File: catch-errors

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (45 lines) | stat: -rw-r--r-- 1,954 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
S9fES  (catch-errors <value> <body>)  ==>  object
       *error-tag*                    ==>  catch-tag
       *error-value*                  ==>  object

CATCH-ERRORS intercepts error conditions as signalled by the
interpreter. First it evaluates <value>, which may be an expression
evaluating to any kind of object and sets up a catch tag (see CATCH)
that will be thrown when an error occurs during the evaluation of
the given <body> (which may contain any kind of expressions). The
dynamic extent of the catch tag is called the "error context" of
the CATCH-ERRORS form.

When no error is signalled during the evaluation of the <body>,
CATCH-ERRORS will return the value of the last expression in <body>.
When an error is signalled, it will return <value> instead. Any
expressions of <body> that would have been evaluated after the error
occurred will never be evaluated in this case.

When CATCH-ERRORS has returned its value, the caller's error
context will be re-established.

When an error is signalled during the evaluation of <value>, the
error is not intercepted and will either be caught in an outer error
context or terminate program evaluation.

The top-level variable *ERROR-TAG* is bound to the catch tag that
will currently be thrown when an error is intercepted. *ERROR-VALUE*
is bound to the value that will be returned by CATCH-ERRORS in case
of an error. These variables should never be modified by procedures
other than CATCH-ERRORS.

Rationale:

CATCH-ERRORS is a general mechanism for intercepting errors when
an error is likely to occur, like reading user input with READ or
dividing integers in computations where a divisor of zero may
appear. Typically, the <body> of CATCH-ERRORS should be small,
because otherwise unexpected errors may be caught and lead to
unexpected program behavior.

(catch-errors 'failed (cons 1 2))  ==>  (1 . 2)

(catch-errors 0 (quotient 1 0))  ==>  0

(catch-errors 'oops (read)) #\undefined  ==> oops