you can do something closer to java by composing a macro on top of with-open . It might look like this:
(defmacro with-open+ [[var-name resource & clauses] & body] (if (seq clauses) `(try (with-open [~var-name ~resource] ~@body ) ~@clauses ) `(with-open [~var-name ~resource] ~@body )))
so that you can submit additional offers along with the binding.
(with-open+ [x 111] (println "body"))
expands to a simple with-open :
(let* [x 111] (try (do (println "body")) (finally (. x clojure.core/close))))
while additional sentences lead to its wrapping in try-catch:
(with-open+ [x 111 (catch RuntimeException ex (println ex)) (finally (println "finally!"))] (println "body"))
expands to
(try (let* [x 111] (try (do (println "body")) (finally (. x clojure.core/close)))) (catch RuntimeException ex (println ex)) (finally (println "finally!")))
But still this is a rather stubborn decision.
source share