Use try+ with advanced catch selectors
releaseThe try+ macro provides enhanced catch clauses that can select exceptions based on more than just class types. A catch clause will execute for the first selector that matches the thrown object.
Supported selector types:
- Class name: (e.g.,
RuntimeException,my.clojure.record) matches any instance of that class. - Key-values vector: (e.g.,
[key val & kvs]) matches objects where(and (= (get object key) val) ...)is true. - Predicate: A function of one argument (e.g.,
map?,set?) that returns a truthy value. - Selector form: A form containing one or more
%instances to be replaced by the thrown object (e.g.,(some-logic %)).
(try+
(do-something)
(catch [:type :tensor.parse/bad-tree] {:keys [tree hint]}
;; matches if (get object :type) is :tensor.parse/bad-tree
(println "Caught bad tree:" tree))
(catch Object _
;; matches any other object
(println "Caught something else")))