stack-catch.scm 1.9 KB

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