* stack-catch.scm: New file.
[bpt/guile.git] / ice-9 / stack-catch.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 2001 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
19 ;;;;
20
21 (define-module (ice-9 stack-catch)
22 :export (stack-catch))
23
24 (define (stack-catch key thunk handler)
25 "Like @code{catch}, invoke @var{thunk} in the dynamic context of
26 @var{handler} for exceptions matching @var{key}, but also save the
27 current stack state in the @var{the-last-stack} fluid, for the purpose
28 of debugging or re-throwing of an error. If thunk throws to the
29 symbol @var{key}, then @var{handler} is invoked this way:\n
30 @example
31 (handler key args ...)
32 @end example\n
33 @var{key} is a symbol or #t.\n
34 @var{thunk} takes no arguments. If @var{thunk} returns normally, that
35 is the return value of @code{catch}.\n
36 Handler is invoked outside the scope of its own @code{catch}. If
37 @var{handler} again throws to the same key, a new handler from further
38 up the call chain is invoked.\n
39 If the key is @code{#t}, then a throw to @emph{any} symbol will match
40 this call to @code{catch}."
41 (catch key
42 (lambda ()
43 (lazy-catch key
44 thunk
45 lazy-handler-dispatch))
46 handler))