example-03-create-new-exception-types.scm 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (import
  2. (ice-9 exceptions)
  3. (srfi srfi-9 gnu))
  4. (define-immutable-record-type <position>
  5. (construct-position row col)
  6. position?
  7. (row position-row set-position-row)
  8. (col position-col set-position-col))
  9. ;; Here we define a new, a custom, exception type, by giving the
  10. ;; constructor of an exception type a name, which conforms to the
  11. ;; style of other exception constructor names.
  12. (define make-inappropriate-value-exception
  13. ;; record-constructor is a procedure, which will return the
  14. ;; constructor for any record.
  15. (record-constructor
  16. ;; Create an exception type, which is a record. This record has a
  17. ;; constructor, which we can name using define for example.
  18. (make-exception-type
  19. ;; name of the new exception type
  20. '&inappropriate-value
  21. ;; parent exception type
  22. &programming-error
  23. ;; list of values the constructor of the exception takes and their
  24. ;; names in the record
  25. '(val))))
  26. (define make-position
  27. (λ (row col)
  28. (cond
  29. [(not
  30. (and (integer? row)
  31. (or (zero? row) (positive? row))))
  32. (raise-exception
  33. (make-exception
  34. (make-inappropriate-value-exception row)
  35. (make-exception-with-message "row must be a zero or a positive integer")
  36. (make-exception-with-irritants (list row))
  37. (make-exception-with-origin 'make-position)))]
  38. [(not
  39. (and (integer? col)
  40. (or (zero? col) (positive? col))))
  41. (raise-exception
  42. (make-exception
  43. (make-inappropriate-value-exception col)
  44. (make-exception-with-message "col must be a zero or a positive integer")
  45. (make-exception-with-irritants (list col))
  46. (make-exception-with-origin 'make-position)))]
  47. [else
  48. (make-position row col)])))
  49. (make-position -1 0)