Programming Clojure, Third Edition: `assert` throws Error, not Exception (page 206-7)

@alexmiller

On page 206 to 207, my copy of Programming Clojure, Third Edition asserts the following:

assert tests an expression and raises an exception if it’s not logically true:

(assert (= 1 1))
 -> nil

 (assert (= 1 2))
 -> java.lang.Exception: Assert failed: (= 1 2)

This matches the assert docstring:

Evaluates expr and throws an exception if it does not evaluate to
logical true.

Unfortunately, it’s not so. assert raises an AssertionError, which is not an Exception. Reference Clojure source, ask Clojure question, JIRA ticket, REPL output:

$ clj
Clojure 1.11.1
user=> (assert (= 1 1))
nil
user=> (assert (= 1 2))
Execution error (AssertionError) at user/eval138 (REPL:1).
Assert failed: (= 1 2)
user=> (set! *assert* false)
false
user=> (assert (= 1 2))
nil

Perhaps the book’s REPL output predates this 2009 commit?

The book and docstring say it “throws an exception” (a Java and Clojure language mechanism), not “throws Exception” (a specific Java class), so I think the statements are correct regardless.

Generally, the docstrings are careful about what they commit to, and there is no commitment here to a specific class.

Fair enough. I recognize that absence of commitment. Still, to me that docstring is an opportunity to redirect sub-optimal tendencies in the broader Clojure community like the one you noted here.

Docstring aside, I’m glad the surprising exception class led me to the macro’s git history. Thanks and cheers.