Add "transient" intmap interface
[bpt/guile.git] / module / ice-9 / stack-catch.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 2001, 2006, 2010 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ;;;;
19
20 (define-module (ice-9 stack-catch)
21 #:use-module (ice-9 save-stack)
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 thunk
43 handler
44 (lambda (key . args)
45 ;; Narrow by two more frames: this one, and the throw handler.
46 (save-stack 2)
47 (apply throw key args))))