简体   繁体   English

如何在Clojure中捕获多个异常?

[英]How to catch multiple exceptions in Clojure?

My Clojure code has some java-interop with a method that throws multiple exceptions. 我的Clojure代码有一些java-interop,它有一个抛出多个异常的方法。 I wish to deal with each individual of them. 我希望与他们中的每个人打交道。 According to Clojure documentation: 根据Clojure文档:

(try expr* catch-clause* finally-clause?)
catch-clause -> (catch classname name expr*)

it has no mention of catching multiple exceptions. 它没有提到捕捉多个例外。 Is it possible to do so in Clojure? 是否有可能在Clojure中这样做?

Thank you! 谢谢!

It's the same as in Java, you can declare several catch expressions one after the other, and they'll get matched in the same order they were declared - first Exception1 , if it doesn't match then Exception2 and so on, and the finally part will always be executed. 它与Java中的相同,你可以一个接一个地声明几个catch表达式,并且它们将按照它们声明的相同顺序进行匹配 - 首先是Exception1 ,如果它与Exception2不匹配,依此类推, finally部分将始终执行。

(try <some code>
    (catch Exception1 e1 (prn "in catch1"))
    (catch Exception2 e2 (prn "in catch2"))
    (finally (prn "in finally")))

In fact, this is specified in the documentation, (try expr* catch-clause* finally-clause?) means that you can have "zero or more expressions", "zero or more catch clauses" and "zero or one finally clauses" as part of a try expression. 实际上,这是在文档中指定的, (try expr* catch-clause* finally-clause?)意味着你可以拥有“零个或多个表达式”,“零个或多个catch子句”和“零个或一个finally子句”作为try表达式的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM