1.3.0 permalink Arrow_down_16x16

assert

clojure.core

  • (assert x)
  • (assert x message)
Evaluates expr and throws an exception if it does not evaluate to
logical true.

1 Example top

  • user=> (assert true)
    nil
    
    user=> (assert false)
    java.lang.AssertionError: Assert failed: false (NO_SOURCE_FILE:0)
    
    user=> (assert nil)
    java.lang.AssertionError: Assert failed: nil (NO_SOURCE_FILE:0)
    
    user=> (assert 0)
    nil
    
    user=> (assert [1 2 3])
    nil
    
    user=> (assert "foo")
    nil
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4222 top

(defmacro assert
  "Evaluates expr and throws an exception if it does not evaluate to
  logical true."
  {:added "1.0"}
  ([x]
     (when *assert*
       `(when-not ~x
          (throw (new AssertionError (str "Assert failed: " (pr-str '~x)))))))
  ([x message]
     (when *assert*
       `(when-not ~x
          (throw (new AssertionError (str "Assert failed: " ~message "\n" (pr-str '~x))))))))
Vars in clojure.core/assert:
Used in 0 other vars

Comments top

1 comment(s) for assert.

The documentation ("... and throws an exception if ...") is incorrect. It throws AssertionError which an Error and not an Exception. They both extend Throwable but Error is not an Exception.

Log in to add a comment.