Clojure中多个函数调用的错误处理
我一直在努力寻找一种在Clojure中正确处理错误的方法,并且喜欢关于这个主题的一些想法 给出了一个没有错误处理的示例:Clojure中多个函数调用的错误处理,clojure,Clojure,我一直在努力寻找一种在Clojure中正确处理错误的方法,并且喜欢关于这个主题的一些想法 给出了一个没有错误处理的示例: (defn do-stuff (let [result1 (some-function) result2 (other-function) result3 (yet-another-function)] {:status 200 :body (foo result1 result2 result3)})) 如果某个地
(defn do-stuff
(let [result1 (some-function)
result2 (other-function)
result3 (yet-another-function)]
{:status 200
:body (foo result1 result2 result3)}))
如果某个地方出现错误,应返回以下内容:
{:status 4xx
:body "Some descriptive error message, based on what went wrong"}
如何确保结果1-3在传递给foo之前有效
如果let块中的某个函数出现错误(假设这些函数中没有正确的方法来处理错误),它们是否应该抛出异常以在do stuff中处理?如果它们抛出异常,您可以在let之外捕获它们:
(defn do-stuff
(try
(let [result1 (some-function)
result2 (other-function)
result3 (yet-another-function)]
{:status 200
:body (foo result1 result2 result3)})
(catch MyException e
{:status 4xx
:body (str "Some descriptive error message, " (.getMessage e)})