2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-10-23 23:34:11 -04:00
|
|
|
|
|
|
|
(library (ikarus exceptions)
|
2007-10-26 15:27:42 -04:00
|
|
|
(export with-exception-handler raise raise-continuable
|
2009-08-03 03:36:18 -04:00
|
|
|
error warning assertion-violation die)
|
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
|
2007-10-26 15:27:42 -04:00
|
|
|
make-irritants-condition make-assertion-violation)
|
2007-10-23 23:34:11 -04:00
|
|
|
(except (ikarus)
|
2007-10-26 15:27:42 -04:00
|
|
|
with-exception-handler raise raise-continuable
|
2009-08-03 03:36:18 -04:00
|
|
|
error warning assertion-violation die))
|
2007-10-23 23:34:11 -04:00
|
|
|
|
|
|
|
(define handlers
|
|
|
|
(make-parameter
|
|
|
|
(list
|
|
|
|
(lambda (x)
|
2007-12-10 07:40:34 -05:00
|
|
|
(display "Unhandled exception:\n" (console-error-port))
|
|
|
|
(print-condition x (console-error-port))
|
2008-03-25 21:38:11 -04:00
|
|
|
(when (serious-condition? x)
|
2008-06-07 11:32:00 -04:00
|
|
|
(exit -1)))
|
|
|
|
(lambda args (exit -1)))))
|
2007-10-23 23:34:11 -04:00
|
|
|
|
|
|
|
(define (with-exception-handler handler proc2)
|
|
|
|
(unless (procedure? handler)
|
2007-12-15 08:22:49 -05:00
|
|
|
(assertion-violation '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-12-15 08:22:49 -05:00
|
|
|
(assertion-violation '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
|
|
|
|
2009-08-03 03:36:18 -04:00
|
|
|
(define (err who* raise* cond* who msg irritants)
|
|
|
|
(unless (string? msg)
|
|
|
|
(assertion-violation who* "message is not a string" msg))
|
|
|
|
(raise*
|
2007-10-24 00:24:38 -04:00
|
|
|
(condition
|
2009-08-03 03:36:18 -04:00
|
|
|
(cond*)
|
2007-10-24 00:24:38 -04:00
|
|
|
(if who (make-who-condition who) (condition))
|
|
|
|
(make-message-condition msg)
|
2008-07-26 17:11:22 -04:00
|
|
|
(make-irritants-condition irritants))))
|
2007-10-23 23:55:57 -04:00
|
|
|
|
2009-08-03 03:36:18 -04:00
|
|
|
(define (error who msg . irritants)
|
|
|
|
(err 'error raise make-error who msg irritants))
|
|
|
|
(define (assertion-violation who msg . irritants)
|
|
|
|
(err 'assertion-violation raise make-assertion-violation who msg irritants))
|
|
|
|
(define (warning who msg . irritants)
|
|
|
|
(err 'warning raise-continuable make-warning who msg irritants))
|
|
|
|
(define (die who msg . irritants)
|
|
|
|
(err 'die raise make-assertion-violation who msg irritants))
|
2007-12-15 08:22:49 -05:00
|
|
|
|
2007-10-23 23:34:11 -04:00
|
|
|
)
|
|
|
|
|