guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trigger action at exit?


From: Ludovic Courtès
Subject: Re: Trigger action at exit?
Date: Mon, 03 Mar 2008 22:40:06 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Hi,

"John Trammell" <address@hidden> writes:

> (define (cleanup)
>   (simple-format #t "has-plan: ~a~%" has-plan)
>   (simple-format #t "1..~a~%" index))
>
> (catch 'quit
>        (lambda () (exit 1))
>        (lambda (key .  args)
>          (begin (format #t "in handler~%")
>                 (format #t "key:~a args:~a~%" key args)
>                 (cleanup))))
>
> (set! has-plan #t)
> (format #t "fiddle dee dee~%")

No, you would want to enclose the body of your code in `catch', i.e.,

  (catch 'quit
     (lambda ()
       ;; your code
       ...)
     (lambda (key . args)
       ...))

Anyway, this isn't good, because if your code doesn't call `exit', then
the `quit' exception is never raised.

`dynamic-wind' provides a better solution, since the "handler" is called
whenever the dynamic extent of the body is left:

  (dynamic-wind
    (lambda ()
      ;; nothing to do here
      #t)
    (lambda ()
      ;; the code body
      ;; ...
      )
    (lambda ()
      ;; the "exit guard" or "handler", which gets called whenever the
      ;; body's extent is left, including via an `exit' call
      ))

If your code uses C code that may exit in other ways (e.g., C code that
calls `exit(3)' or similar), then you need an `atexit' similar to what
Neil described.

Thanks,
Ludovic.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]