1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
;;; Exceptions
;;; Copyright (C) 2019 Free Software Foundation, Inc.
;;;
;;; This library is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This library 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
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;;;
;;; Definition of the standard exception types.
;;;
;;; Code:
(define-module (ice-9 exceptions)
#:export (&exception
make-exception
make-exception-type
simple-exceptions
exception?
exception-type?
exception-predicate
exception-accessor
define-exception-type
&message
make-exception-with-message
exception-with-message?
exception-message
&warning
make-warning
warning?
&error
make-error
error?
&external-error
make-external-error
external-error?
&programming-error
make-programming-error
programming-error?
&assertion-failure
make-assertion-failure
assertion-failure?
&irritants
make-exception-with-irritants
exception-with-irritants?
exception-irritants
&origin
make-exception-with-origin
exception-with-origin?
exception-origin
&non-continuable
make-non-continuable-error
non-continuable-error?
&implementation-restriction
make-implementation-restriction-error
implementation-restriction-error?
&lexical
make-lexical-error
lexical-error?
&syntax
make-syntax-error
syntax-error?
syntax-error-form
syntax-error-subform
&undefined-variable
make-undefined-variable-error
undefined-variable-error?
with-exception-handler
raise-exception
raise-continuable))
(define &exception (make-record-type '&exception '() #:extensible? #t))
(define simple-exception? (record-predicate &exception))
(define &compound-exception (make-record-type '&compound-exception
'((immutable components))))
(define compound-exception? (record-predicate &compound-exception))
(define make-compound-exception (record-constructor &compound-exception))
(define simple-exceptions
(let ((compound-ref (record-accessor &compound-exception 'components)))
(lambda (exception)
"Return a list of the simple exceptions that compose the exception
object @var{exception}."
(cond ((compound-exception? exception)
(compound-ref exception))
((simple-exception? exception)
(list exception))
(else
(error "not a exception" exception))))))
(define make-exception
(lambda exceptions
(let ((simple
(let flatten ((exceptions exceptions))
(if (null? exceptions)
'()
(append (simple-exceptions (car exceptions))
(flatten (cdr exceptions)))))))
(if (and (pair? simple) (null? (cdr simple)))
(car simple)
(make-compound-exception simple)))))
(define (exception? obj)
"Return true if @var{obj} is an exception."
(or (compound-exception? obj) (simple-exception? obj)))
(define (exception-type? obj)
"Return true if OBJ is an exception type."
(and (record-type? obj)
(record-type-has-parent? obj &exception)))
(define (make-exception-type id parent field-names)
"Return a new exception type named @var{id}, inheriting from
@var{parent}, and with the fields whose names are listed in
@var{field-names}. @var{field-names} must be a list of symbols and must
not contain names already used by @var{parent} or one of its
supertypes."
(unless (exception-type? parent)
(error "parent is not a exception type" parent))
(unless (and-map symbol? field-names)
(error "field names should be a list of symbols" field-names))
(make-record-type id field-names #:parent parent #:extensible? #t))
(define (exception-predicate rtd)
"Return a procedure that will return true if its argument is a simple
exception that is an instance of @var{rtd}, or a compound exception
composed of such an instance."
(let ((rtd-predicate (record-predicate rtd)))
(lambda (obj)
(cond ((compound-exception? obj)
(or-map rtd-predicate (simple-exceptions obj)))
(else (rtd-predicate obj))))))
(define (exception-accessor rtd proc)
(let ((rtd-predicate (record-predicate rtd)))
(lambda (obj)
(if (rtd-predicate obj)
(proc obj)
(let lp ((exceptions (if (compound-exception? obj)
(simple-exceptions obj)
'())))
(when (null? exceptions)
(error "object is not an exception of the right type" obj rtd))
(if (rtd-predicate (car exceptions))
(proc (car exceptions))
(lp (cdr exceptions))))))))
(define-syntax define-exception-type
(syntax-rules ()
((_ exception-type supertype constructor predicate
(field accessor) ...)
(begin
(define exception-type
(make-record-type 'exception-type '((immutable field) ...)
#:parent supertype #:extensible? #t))
(define constructor (record-constructor exception-type))
(define predicate (exception-predicate exception-type))
(define accessor
(exception-accessor exception-type
(record-accessor exception-type 'field)))
...))))
(define-exception-type &error &exception
make-error error?)
(define-exception-type &programming-error &error
make-programming-error programming-error?)
(define-exception-type &assertion-failure &programming-error
make-assertion-failure assertion-failure?)
(define-exception-type &message &exception
make-exception-with-message exception-with-message?
(message exception-message))
(define-exception-type &warning &exception
make-warning warning?)
(define-exception-type &external-error &error
make-external-error external-error?)
(define-exception-type &irritants &exception
make-exception-with-irritants exception-with-irritants?
(irritants exception-irritants))
(define-exception-type &origin &exception
make-exception-with-origin exception-with-origin?
(origin exception-origin))
(define-exception-type &non-continuable &programming-error
make-non-continuable-error
non-continuable-error?)
(define-exception-type &implementation-restriction &programming-error
make-implementation-restriction-error
implementation-restriction-error?)
(define-exception-type &lexical &programming-error
make-lexical-error lexical-error?)
(define-exception-type &syntax &programming-error
make-syntax-error syntax-error?
(form syntax-error-form)
(subform syntax-error-subform))
(define-exception-type &undefined-variable &programming-error
make-undefined-variable-error undefined-variable-error?)
;; When a native guile exception is caught by with-exception-handler, we
;; convert it to a compound exception that includes not only the
;; standard exception objects expected by users of R6RS, SRFI-35, and
;; R7RS, but also a special &guile condition that preserves the original
;; KEY and ARGS passed to the native Guile catch handler.
(define-exception-type &guile &exception
make-guile-exception guile-exception?
(key guile-exception-key)
(args guile-exception-args))
(define (default-guile-exception-converter key args)
(make-exception (make-error)
(guile-common-exceptions key args)))
(define (guile-common-exceptions key args)
(apply (case-lambda
((subr msg margs . _)
(make-exception
(make-exception-with-origin subr)
(make-exception-with-message msg)
(make-exception-with-irritants margs)))
(_ (make-exception-with-irritants args)))
args))
(define (convert-guile-exception key args)
(let ((converter (assv-ref guile-exception-converters key)))
(make-exception (or (and converter (converter key args))
(default-guile-exception-converter key args))
;; Preserve the original KEY and ARGS in the R6RS
;; exception object.
(make-guile-exception key args))))
;; If an exception handler chooses not to handle a given exception, it
;; will re-raise the exception to pass it on to the next handler. If
;; the exception was converted from a native Guile exception, we must
;; re-raise using the native Guile facilities and the original exception
;; KEY and ARGS. We arrange for this in 'raise' so that native Guile
;; exception handlers will continue to work when mixed with
;; with-exception-handler.
(define &raise-object-wrapper
(make-record-type '&raise-object-wrapper
'((immutable obj) (immutable continuation))))
(define make-raise-object-wrapper
(record-constructor &raise-object-wrapper))
(define raise-object-wrapper?
(record-predicate &raise-object-wrapper))
(define raise-object-wrapper-obj
(record-accessor &raise-object-wrapper 'obj))
(define raise-object-wrapper-continuation
(record-accessor &raise-object-wrapper 'continuation))
(define (raise-exception obj)
(if (guile-exception? obj)
(apply throw (guile-exception-key obj) (guile-exception-args obj))
(throw '%exception (make-raise-object-wrapper obj #f))))
(define (raise-continuable obj)
(call/cc
(lambda (k)
(throw '%exception (make-raise-object-wrapper obj k)))))
(define (with-exception-handler handler thunk)
(with-throw-handler #t
thunk
(lambda (key . args)
(cond ((not (eq? key '%exception))
(let ((obj (convert-guile-exception key args)))
(handler obj)
(raise-exception (make-non-continuable-error))))
((and (not (null? args))
(raise-object-wrapper? (car args)))
(let* ((cargs (car args))
(obj (raise-object-wrapper-obj cargs))
(continuation (raise-object-wrapper-continuation cargs))
(handler-return (handler obj)))
(if continuation
(continuation handler-return)
(raise-exception (make-non-continuable-error)))))))))
;;; Exception printing
(define (exception-printer port key args punt)
(cond ((and (= 1 (length args))
(raise-object-wrapper? (car args)))
(let ((obj (raise-object-wrapper-obj (car args))))
(cond ((exception? obj)
(display "ERROR:\n" port)
(format-exception port obj))
(else
(format port "ERROR: `~s'" obj)))))
(else
(punt))))
(define (format-exception port exception)
(let ((components (simple-exceptions exception)))
(if (null? components)
(format port "Empty exception object")
(let loop ((i 1) (components components))
(cond ((pair? components)
(format port " ~a. " i)
(format-simple-exception port (car components))
(when (pair? (cdr components))
(newline port))
(loop (+ i 1) (cdr components))))))))
(define (format-simple-exception port exception)
(let* ((type (struct-vtable exception))
(name (record-type-name type))
(fields (record-type-fields type)))
(cond
((null? fields)
(format port "~a" name))
((null? (cdr fields))
(format port "~a: ~s" name (struct-ref exception 0)))
(else
(format port "~a:\n" name)
(let lp ((fields fields) (i 0))
(let ((field (car fields))
(fields (cdr fields)))
(format port " ~a: ~s" field (struct-ref exception i))
(unless (null? fields)
(newline port)
(lp fields (+ i 1)))))))))
(set-exception-printer! '%exception exception-printer)
;; Guile exception converters
;;
;; Each converter is a procedure (converter KEY ARGS) that returns
;; either an exception object or #f. If #f is returned,
;; 'default-guile-exception-converter' will be used.
(define (guile-syntax-error-converter key args)
(apply (case-lambda
((who what where form subform . extra)
(make-exception (make-syntax-error form subform)
(make-exception-with-origin who)
(make-exception-with-message what)))
(_ #f))
args))
(define (guile-lexical-error-converter key args)
(make-exception (make-lexical-error)
(guile-common-exceptions key args)))
(define (guile-assertion-failure-converter key args)
(make-exception (make-assertion-failure)
(guile-common-exceptions key args)))
(define (guile-undefined-variable-error-converter key args)
(make-exception (make-undefined-variable-error)
(guile-common-exceptions key args)))
(define (guile-implementation-restriction-converter key args)
(make-exception (make-implementation-restriction-error)
(guile-common-exceptions key args)))
(define (guile-external-error-converter key args)
(make-exception (make-external-error)
(guile-common-exceptions key args)))
(define (guile-system-error-converter key args)
(apply (case-lambda
((subr msg msg-args errno . rest)
;; XXX TODO we should return a more specific error
;; (usually an I/O error) as expected by R6RS programs.
;; Unfortunately this often requires the 'filename' (or
;; other?) which is not currently provided by the native
;; Guile exceptions.
(make-exception (make-external-error)
(guile-common-exceptions key args)))
(_ (guile-external-error-converter key args)))
args))
;; TODO: Arrange to have the needed information included in native
;; Guile I/O exceptions, and arrange here to convert them to the
;; proper exceptions. Remove the earlier exception conversion
;; mechanism: search for 'with-throw-handler' in the 'rnrs'
;; tree, e.g. 'with-i/o-filename-exceptions' and
;; 'with-i/o-port-error' in (rnrs io ports).
;; XXX TODO: How should we handle the 'misc-error', 'vm-error', and
;; 'signal' native Guile exceptions?
;; XXX TODO: Should we handle the 'quit' exception specially?
;; An alist mapping native Guile exception keys to converters.
(define guile-exception-converters
`((read-error . ,guile-lexical-error-converter)
(syntax-error . ,guile-syntax-error-converter)
(unbound-variable . ,guile-undefined-variable-error-converter)
(wrong-number-of-args . ,guile-assertion-failure-converter)
(wrong-type-arg . ,guile-assertion-failure-converter)
(keyword-argument-error . ,guile-assertion-failure-converter)
(out-of-range . ,guile-assertion-failure-converter)
(regular-expression-syntax . ,guile-assertion-failure-converter)
(program-error . ,guile-assertion-failure-converter)
(goops-error . ,guile-assertion-failure-converter)
(null-pointer-error . ,guile-assertion-failure-converter)
(system-error . ,guile-system-error-converter)
(host-not-found . ,guile-external-error-converter)
(getaddrinfo-error . ,guile-external-error-converter)
(no-data . ,guile-external-error-converter)
(no-recovery . ,guile-external-error-converter)
(try-again . ,guile-external-error-converter)
(stack-overflow . ,guile-implementation-restriction-converter)
(numerical-overflow . ,guile-implementation-restriction-converter)
(memory-allocation-error . ,guile-implementation-restriction-converter)))
(define (set-guile-exception-converter! key proc)
(set! guile-exception-converters
(acons key proc guile-exception-converters)))
|