O'Reilly logo

Mastering Clojure Macros by Colin Jones

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required