|
191 | 191 | ;; new style uses |
192 | 192 | with-exception-handler raise-exception |
193 | 193 |
|
| 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 | + |
194 | 200 | @lisp{ |
195 | 201 | ;; try-catch |
196 | 202 | (begin |
197 | 203 | (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") |
201 | 207 | (/ 1 0) |
202 | 208 | (format #t "[try] Unreachable\n")) |
203 | 209 | (format #t "Moving on.\n")) |
204 | 210 |
|
205 | | - ;; ignore exception |
| 211 | + ;; try-catch with fat arrow macro |
206 | 212 | (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")) |
209 | 221 | (format #t "Moving on.\n")) |
210 | 222 |
|
211 | 223 | ;; See https://vijaymarupudi.com/blog/2022-02-13-error-handling-in-guile.html |
|
219 | 231 | (read-reason read-exception-reason) |
220 | 232 | (read-severity read-exception-severity)) |
221 | 233 | ;; |
| 234 | + ;; try-catch guard |
222 | 235 | (with-exception-handler |
223 | 236 | (lambda (exception) |
224 | 237 | (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" |
228 | 241 | (disk-space-amount)) |
229 | | - (format #t "please provide a different file size: ") |
| 242 | + (format #t "[catch] Please provide a different file size: ") |
230 | 243 | (let ((new-file-size (read))) |
231 | 244 | (if (disk-space-left? new-file-size) |
232 | 245 | new-file-size |
233 | | - (raise-exception exception)))) |
234 | | - (else (raise-exception exception)))) |
| 246 | + (raise-exception exception)))] |
| 247 | + [else (raise-exception exception)])) |
235 | 248 | (lambda () |
236 | | - (let ((file-size (if (disk-space-left? 1028) |
| 249 | + (let [(file-size (if (disk-space-left? 1028) |
237 | 250 | 1028 |
238 | 251 | (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))))) |
241 | 254 |
|
242 | 255 | (throw 'my-exception) |
243 | 256 | (throw 'my-exception "Some description of <my-exception>") |
|
0 commit comments