Rescuing Errors

Another common use case for macros is to limit the reach of any errors we encounter during evaluation. Unit testing libraries typically have to do this in order to capture failures while continuing to run other tests. In clojure.test, for instance, the is macro uses an internal macro try-expr to catch exceptions and report them to the test-running infrastructure.

context/try_expr.clj
 
(​defmacro​ try-expr [msg form]
 
`(​try​ ~(assert-expr msg form)
 
(​catch​ Throwable t#
 
(do-report {:type :error, :message ~msg,
 
:expected '~form, :actual t#}))))
 
(​defmacro​ is
 
([form] `(is ~form nil))
 
([form msg] `(try-expr ~msg ~form)))

So when the form is actually evaluated within the assert-expr, if an unexpected exception propagates ...

Get Mastering Clojure Macros now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.