example-02-guile-compound-exceptions.scm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (use-modules
  2. (ice-9 exceptions))
  3. ;; =========
  4. ;; UTILITIES
  5. ;; =========
  6. (define even-simpler-display
  7. (lambda (sth)
  8. (display
  9. (simple-format
  10. #f "~a\n" sth))))
  11. ;; =====
  12. ;; INTRO
  13. ;; =====
  14. ;; Guile compound exceptions are similar to rnrs exceptions (conditions) in
  15. ;; concept, but have different names for procedures and simple (non-compound)
  16. ;; exceptions. The way one accesses the simple exceptions inside a compound
  17. ;; exception also differs.
  18. ;; ===================
  19. ;; COMPOUND EXCEPTIONS
  20. ;; ===================
  21. (define divide
  22. (lambda (dividend divisor)
  23. (cond
  24. [(= divisor 0)
  25. (raise-exception
  26. (make-exception
  27. (make-non-continuable-error)
  28. (make-exception-with-message "division by zero")
  29. (make-exception-with-irritants (list dividend divisor))
  30. (make-exception-with-origin 'divide)))]
  31. [else
  32. (/ dividend divisor)])))
  33. (with-exception-handler
  34. (lambda (exception)
  35. (display
  36. (simple-format
  37. #f "warning?: ~a\n" (warning? exception)))
  38. (display
  39. (simple-format
  40. #f "exception?: ~a\n" (exception? exception)))
  41. (display
  42. (simple-format
  43. #f "exception-with-message?: ~a\n"
  44. (exception-with-message? exception)))
  45. (display
  46. (simple-format
  47. #f "exception-message: ~a\n"
  48. (exception-message exception)))
  49. (display
  50. (simple-format
  51. #f "exception: ~a\n" exception)))
  52. (lambda ()
  53. (divide 2 0))
  54. #:unwind? #t)