2

Here is the passing version of the code:

Normal function: That passes

(defn my-fn
  []
  (throw (IllegalStateException.)))

(fact
  (my-fn) => (throws IllegalStateException))

Here is the macro version of it:

(defmacro my-fn
  []
  (throw (IllegalStateException.)))

(fact
  (my-fn) => (throws IllegalStateException))

Which fails here is the output:

LOAD FAILURE for example.dsl.is-test
java.lang.IllegalStateException, compiling:(example/dsl/is_test.clj:14:3)
The exception has been stored in #'*me, so `(pst *me)` will show the stack trace.
FAILURE: 1 check failed.  (But 12 succeeded.)

It is the same code I just changed defn to defmacro.

I did not understand that why this is not working?

Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76

1 Answers1

4

the thing is your macro is wrong. While your function was throwing an error in runtime, the macro throws it in compile-time. The following should fix that behavior:

(defmacro my-fn
  []
  `(throw (IllegalStateException.)))

now your macro call would be substituted with the exception being thrown. Something like that:

(fact
  (throw (IllegalStateException.)) => (throws IllegalStateException))
leetwinski
  • 17,408
  • 2
  • 18
  • 42