2007-10-23 23:34:11 -04:00
|
|
|
|
|
|
|
(library (ikarus exceptions)
|
2007-10-24 00:24:38 -04:00
|
|
|
(export with-exception-handler raise raise-continuable error)
|
2007-10-23 23:34:11 -04:00
|
|
|
(import
|
|
|
|
(only (rnrs) condition make-non-continuable-violation
|
2007-10-23 23:55:57 -04:00
|
|
|
make-message-condition make-error make-who-condition
|
|
|
|
make-irritants-condition)
|
2007-10-23 23:34:11 -04:00
|
|
|
(except (ikarus)
|
2007-10-23 23:55:57 -04:00
|
|
|
with-exception-handler raise raise-continuable error))
|
2007-10-23 23:34:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
(define handlers
|
|
|
|
(make-parameter
|
|
|
|
(list
|
|
|
|
(lambda (x)
|
|
|
|
(printf "unhandled exception:\n")
|
|
|
|
(print-condition x)
|
|
|
|
(exit -1)))))
|
|
|
|
|
|
|
|
(define (with-exception-handler handler proc2)
|
|
|
|
(unless (procedure? handler)
|
|
|
|
(error 'with-exception-handler
|
2007-10-25 14:32:26 -04:00
|
|
|
"handler is not a procedure" handler))
|
2007-10-23 23:34:11 -04:00
|
|
|
(unless (procedure? proc2)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-exception-handler "not a procedure" proc2))
|
2007-10-23 23:34:11 -04:00
|
|
|
(parameterize ([handlers (cons handler (handlers))])
|
|
|
|
(proc2)))
|
|
|
|
|
|
|
|
(define (raise-continuable x)
|
|
|
|
(let ([h* (handlers)])
|
|
|
|
(let ([h (car h*)] [h* (cdr h*)])
|
|
|
|
(parameterize ([handlers h*])
|
|
|
|
(h x)))))
|
|
|
|
|
|
|
|
(define (raise x)
|
|
|
|
(let ([h* (handlers)])
|
|
|
|
(let ([h (car h*)] [h* (cdr h*)])
|
|
|
|
(parameterize ([handlers h*])
|
|
|
|
(h x)
|
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-non-continuable-violation)
|
|
|
|
(make-message-condition "handler returned")))))))
|
2007-10-23 23:55:57 -04:00
|
|
|
|
|
|
|
(define (error who msg . irritants)
|
|
|
|
(unless (string? msg)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'error "message is not a string" msg))
|
2007-10-23 23:55:57 -04:00
|
|
|
(raise
|
2007-10-24 00:24:38 -04:00
|
|
|
(condition
|
|
|
|
(make-error)
|
|
|
|
(if who (make-who-condition who) (condition))
|
|
|
|
(make-message-condition msg)
|
|
|
|
(if (null? irritants)
|
|
|
|
(condition)
|
|
|
|
(make-irritants-condition irritants)))))
|
2007-10-23 23:55:57 -04:00
|
|
|
|
|
|
|
|
2007-10-23 23:34:11 -04:00
|
|
|
)
|
|
|
|
|