下面是代码的传递版本:
正规函数:通过的
(defn my-fn
[]
(throw (IllegalStateException.)))
(fact
(my-fn) => (throws IllegalStateException))
这里是它的宏版本:
(defmacro my-fn
[]
(throw (IllegalStateException.)))
(fact
(my-fn) => (throws IllegalStateException))
哪个失败了,,这里是输出:
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.)
我刚才将defn更改为Def宏的代码与此相同。
我不明白为何这是行不通的呢?
发布于 2016-11-17 13:34:28
问题是你的宏是错的。当您的函数在运行时抛出错误时,宏在编译时抛出它。以下应该修复此行为:
(defmacro my-fn
[]
`(throw (IllegalStateException.)))
现在,宏调用将被替换为抛出的异常。就像这样:
(fact
(throw (IllegalStateException.)) => (throws IllegalStateException))
https://stackoverflow.com/questions/40664590
复制相似问题