Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Compiler by

try silently accepts multiple catch blocks for the same class, but only the first one gets called

user=> (try (/ 1 0) (catch Exception _ (println "a")) (catch Exception _ (println "b"))) a nil

2 Answers

0 votes
by

Comment made by: schmee

I've attached a patch which fixes this problem by bringing Clojure's try/catch semantics more inline with Java. For each catch clause, the compiler checks if a previous catch clause is a supertype if the current one. If it is, an IllegalArgumentException is thrown. This is O(n^2) in the number of catch clauses, but since n will be low in the vast majority of cases, and since the work is done at parse time, it should not pose any performance issues.

I had to modify an unrelated test since it actually had multiple catch clauses that caught {{Exception}} which is no longer allowed with this patch.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2181 (reported by alex+import)
...