; replace exceptions with defaults
; response to chaw

(import (rnrs exceptions (6)))

(define-syntax try
  (syntax-rules (trying)
    ((try trying expr default)
      (call-with-current-continuation
        (lambda (return)
          (with-exception-handler
            (lambda (x) (return default))
            (lambda () expr)))))
    ((try) #f)
    ((try expr) (try trying expr #f))
    ((try expr0 expr1 ...)
      (let ((t (try trying expr0 #f)))
        (if t t (try expr1 ...))))))

(display (try (car '(2)) (begin (display "hello") (newline) 3))) (newline) (newline)

(define (make-try/default proc dflt)
  (lambda args
    (guard (exc (else dflt))
      (apply proc args))))

(define-syntax try/default
  (syntax-rules ()
    ((_  dflt body body1 ...)
     ((make-try/default (lambda () body body1 ...) dflt)))))

(display (try/default (begin (display "hello") (newline) 3) (car '(2)))) (newline)