[plt-scheme] Exception handling outside of Dr Scheme

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Jan 30 09:37:42 EST 2008

At Wed, 30 Jan 2008 06:21:14 -0800, "Vincent Rayappa" wrote:
> Hello,
> If I am using PLT Scheme outside of Dr. Scheme (via the Unix shebang
> notation), how do I get stack traces from a 'handled exception'?
> For example:
> (define (handle-error-p exn) #t)
> (define (exception-handler exn)
>     (printf "Exception caught: ~a ~n" exn)
>     ;; --- How can I print the stack trace here inside the handler?
>     (do-clean-up))
> (with-handlers
>   ((handle-error-p exception-handler))
>   (do-something ...)))
> ---
> How would I get the stack track inside exception-handler?

One way is to use the current error display handler to print the
exception, including the trace:

 (define (exception-handler exn)
    (printf "Exception caught:~n")
    ((error-display-handler) (exn-message exn) exn)

If you want to print the trace manually, you can extract it with
`continuation-mark-set->context' on the `continuation-marks' field of
the exception:

  (define (exception-handler exn)
    (printf "Exception caught: ~a\n~s\n" 
            (continuation-mark-set->context (exn-continuation-marks exn)))


Posted on the users mailing list.