如何在Clojure try/catch上返回一个值

Ric*_*fer 0 exception-handling clojure

我在Clojure中有一个函数,我想在发生异常时返回true,就像下面的代码一样;

    (try
        (code)
    (catch Exception e true)))
Run Code Online (Sandbox Code Playgroud)

但是上面的方法给出了以下错误:

ClassCastException java.lang.Boolean不能强制转换为clojure.lang.IFn signal.message/read?(message.clj:12)

它只适用于放置函数:

    (try
        (code)
    (catch Exception e (= 1 1)))
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在Clojure catch上返回一个值?

muh*_*huk 5

测试你的catch使用:

(try
  (throw (RuntimeException.))
  (catch Exception e true)))
Run Code Online (Sandbox Code Playgroud)

运行此代码将返回true.