Skip to content

Commit 2f0fb03

Browse files
committed
notes
1 parent c10a7f3 commit 2f0fb03

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

notes/guix-guile-nix/guile.scrbl

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,33 @@
191191
;; new style uses
192192
with-exception-handler raise-exception
193193

194+
;; https://codeberg.org/ZelphirKaltstahl/guile-examples/src/branch/master/exception-handling/example-01-rnrs-exceptions-conditions.scm
195+
;; R6RS's or R7RS's guard expression is a wrapper around with-exception-handler
196+
;; with #:unwind? set to #t. It is a macro enabling some convenience. The guard
197+
;; expression allows for a cond expression, which enables to specify different
198+
;; handlers for different exception.
199+
194200
@lisp{
195201
;; try-catch
196202
(begin
197203
(use-modules (ice-9 exceptions))
198-
(guard (exception (else (format #t "[catch] An exception was thrown:\n")
199-
(format #t "[catch] ~a\n\n" exception)))
200-
(format #t "[try]")
204+
(guard (condition
205+
[else (format #t "[catch else] condition: ~a\n" condition)])
206+
(format #t "[try] ...\n")
201207
(/ 1 0)
202208
(format #t "[try] Unreachable\n"))
203209
(format #t "Moving on.\n"))
204210

205-
;; ignore exception
211+
;; try-catch with fat arrow macro
206212
(begin
207-
(use-modules (ice-9 exceptions))
208-
(guard (exception (else #f)) (/ 1 0))
213+
(guard (condition
214+
[(error? condition)
215+
=> (lambda (test-value)
216+
(format #t "[catch error] condition: ~a\n" condition)
217+
(format #t "[catch error] test-value: ~a\n" test-value))])
218+
(format #t "[try] ...\n")
219+
(/ 1 0)
220+
(format #t "[try] Unreachable\n"))
209221
(format #t "Moving on.\n"))
210222

211223
;; See https://vijaymarupudi.com/blog/2022-02-13-error-handling-in-guile.html
@@ -219,25 +231,26 @@
219231
(read-reason read-exception-reason)
220232
(read-severity read-exception-severity))
221233
;;
234+
;; try-catch guard
222235
(with-exception-handler
223236
(lambda (exception)
224237
(cond
225-
((and (read-exception? exception)
226-
(eq? (read-exception-reason exception) 'almost-full))
227-
(format #t "the disk is almost full, only has ~a left.\n"
238+
[(and (read-exception? exception)
239+
(eq? (read-exception-reason exception) 'almost-full))
240+
(format #t "[catch] The disk is almost full, only has ~a left.\n"
228241
(disk-space-amount))
229-
(format #t "please provide a different file size: ")
242+
(format #t "[catch] Please provide a different file size: ")
230243
(let ((new-file-size (read)))
231244
(if (disk-space-left? new-file-size)
232245
new-file-size
233-
(raise-exception exception))))
234-
(else (raise-exception exception))))
246+
(raise-exception exception)))]
247+
[else (raise-exception exception)]))
235248
(lambda ()
236-
(let ((file-size (if (disk-space-left? 1028)
249+
(let [(file-size (if (disk-space-left? 1028)
237250
1028
238251
(raise-continuable
239-
(make-read-exception 'almost-full 'medium)))))
240-
(format #t "writing ~a\n" file-size))))
252+
(make-read-exception 'almost-full 'medium))))]
253+
(format #t "Writing ~a\n" file-size)))))
241254

242255
(throw 'my-exception)
243256
(throw 'my-exception "Some description of <my-exception>")

0 commit comments

Comments
 (0)