我试图在放弃异常之前多次执行一个函数.但是在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
Run Code Online (Sandbox Code Playgroud)
我能找到的最好的是以下笨拙的解决方案(包装在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))))
Run Code Online (Sandbox Code Playgroud) clojure ×1