;; basic (with-error-handler HANDLER THUNK) usage (with-error-handler (lambda (err) (print err)) (lambda () (print unbound-variable))) ;; re-throwing an exception (with-error-handler (lambda (err) (print "rethrown: " err)) (lambda () (with-error-handler (lambda (err) (raise err)) ; rethrow (lambda () (print unbound-variable))))) ;; from $gauche/ext/dbm/test.scm (define-syntax catch (syntax-rules () ((_ body ...) (with-error-handler (lambda (e) #t) (lambda () body ... #f))))) (catch (print unbound-variable)) ;; CL unwind-protect syntax using dynamic-wind (define-syntax unwind-protect (syntax-rules () ((_ body cleanup ...) (dynamic-wind (lambda () #f) (lambda () body) (lambda () cleanup ...))))) (print ((call/cc (let ((x 'aap)) (lambda (k) (unwind-protect (k (lambda () x)) (set! x 'noot))))))) ; => noot, not aap (catch (unwind-protect (begin (print "1 - happens") (raise 'hell) ; force a non-local exit (print "2 - never happens")) ;; This will _always_ be evaluated, despite the `raise'. (print "3 - cleanup"))) ;; using 'guard' syntax (define-class () ()) (define-class () ()) (guard (exception ((is-a? exception ) (print "foo-error")) ((is-a? exception ) (print "bar-error")) (else (print "other exception: " exception))) (raise (make )) (raise (make )) (set! foo 'bar))