至少有one answered question关于异常的重复。我的问题是为什么Clojure编译器不接受2 2 1
recur
错误“只能从尾部位置重现”。
根据控制流程,此(loop []
(try
(catch Exception _ex
(recur))))
位于尾部位置,因为代码在recur
形式recur
之后不执行任何操作。
答案 0 :(得分:0)
如果在询问“为什么会这样?”,您要求提供历史记录,您可以在clojure@googlegroups.com
的Clojure邮件列表中找到更好的答案。我怀疑原因是编译器作者从未考虑过这个用例。
您可以轻松地模拟您想要的行为,如此示例所示。它没有像上一个问题中的大多数答案那样使用宏:
(loop [count 5]
(newline)
(println "top of loop; count=" count)
(let [caught-ex (try
(println "in try")
(/ 1 0)
false
(catch Exception ex
(println " in catch; count=" count " cause: " (.getMessage ex))
true))]
(when (and caught-ex
(pos? count))
(recur (dec count)))))
结果:
top of loop; count= 5
in try
in catch; count= 5 cause: Divide by zero
top of loop; count= 4
in try
in catch; count= 4 cause: Divide by zero
top of loop; count= 3
in try
in catch; count= 3 cause: Divide by zero
top of loop; count= 2
in try
in catch; count= 2 cause: Divide by zero
top of loop; count= 1
in try
in catch; count= 1 cause: Divide by zero
top of loop; count= 0
in try
in catch; count= 0 cause: Divide by zero