| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus exceptions) |
| 18 |
(export with-exception-handler raise raise-continuable |
| 19 |
error warning assertion-violation die) |
| 20 |
(import |
| 21 |
(only (rnrs) condition make-non-continuable-violation |
| 22 |
make-message-condition make-error make-who-condition |
| 23 |
make-irritants-condition make-assertion-violation) |
| 24 |
(except (ikarus) |
| 25 |
with-exception-handler raise raise-continuable |
| 26 |
error warning assertion-violation die)) |
| 27 |
|
| 28 |
(define handlers |
| 29 |
(make-parameter |
| 30 |
(list |
| 31 |
(lambda (x) |
| 32 |
(display "Unhandled exception:\n" (console-error-port)) |
| 33 |
(print-condition x (console-error-port)) |
| 34 |
(when (serious-condition? x) |
| 35 |
(exit -1))) |
| 36 |
(lambda args (foreign-call "ikrt_exit" -2))))) |
| 37 |
|
| 38 |
(define (with-exception-handler handler proc2) |
| 39 |
(unless (procedure? handler) |
| 40 |
(assertion-violation 'with-exception-handler |
| 41 |
"handler is not a procedure" handler)) |
| 42 |
(unless (procedure? proc2) |
| 43 |
(assertion-violation 'with-exception-handler "not a procedure" proc2)) |
| 44 |
(parameterize ([handlers (cons handler (handlers))]) |
| 45 |
(proc2))) |
| 46 |
|
| 47 |
(define (raise-continuable x) |
| 48 |
(let ([h* (handlers)]) |
| 49 |
(let ([h (car h*)] [h* (cdr h*)]) |
| 50 |
(parameterize ([handlers h*]) |
| 51 |
(h x))))) |
| 52 |
|
| 53 |
(define (raise x) |
| 54 |
(let ([h* (handlers)]) |
| 55 |
(let ([h (car h*)] [h* (cdr h*)]) |
| 56 |
(parameterize ([handlers h*]) |
| 57 |
(h x) |
| 58 |
(raise |
| 59 |
(condition |
| 60 |
(make-non-continuable-violation) |
| 61 |
(make-message-condition "handler returned"))))))) |
| 62 |
|
| 63 |
(define (err who* raise* cond* who msg irritants) |
| 64 |
(unless (string? msg) |
| 65 |
(assertion-violation who* "message is not a string" msg)) |
| 66 |
(raise* |
| 67 |
(condition |
| 68 |
(cond*) |
| 69 |
(if who (make-who-condition who) (condition)) |
| 70 |
(make-message-condition msg) |
| 71 |
(make-irritants-condition irritants)))) |
| 72 |
|
| 73 |
(define (error who msg . irritants) |
| 74 |
(err 'error raise make-error who msg irritants)) |
| 75 |
(define (assertion-violation who msg . irritants) |
| 76 |
(err 'assertion-violation raise make-assertion-violation who msg irritants)) |
| 77 |
(define (warning who msg . irritants) |
| 78 |
(err 'warning raise-continuable make-warning who msg irritants)) |
| 79 |
(define (die who msg . irritants) |
| 80 |
(err 'die raise make-assertion-violation who msg irritants)) |
| 81 |
|
| 82 |
) |
| 83 |
|