【发布时间】:2009-12-10 09:44:23
【问题描述】:
我试图在放弃异常之前多次执行一个函数。 但是在 Clojure 中从 catch 块中递归是无效的。 如何实现?
(loop [tries 10]
(try
(might-throw-exception)
(catch Exception e
(when (pos? tries) (recur (dec tries))))))
java.lang.UnsupportedOperationException: Cannot recur from catch/finally
我能找到的最好的是以下笨拙的解决方案(包装在 func 中并调用它)
(defn do-it []
(try
(might-throw-exception)
(catch Exception e nil)))
(loop [times 10]
(when (and (nil? (do-it)) (pos? times))
(recur (dec times))))
【问题讨论】:
标签: clojure