ka <sancha...@gmail.com> writes:

> How can I do something like ...
>
> (try
>   (let [conn (API/getConnection ..)]
>     (....))
>   (catch ..)
>   (finally (if conn (API/closeConnection conn))))
>
> Problem is that conn is unavailable in the finally context.

Why not just have two try blocks?

(try
 (let [conn (API/getConnection ..)]
   (try
    (....)
    (finally (API/closeConnection conn))))
 (catch ...))

For these sorts of things I usually try to use with-open, or if
with-open is not applicable do my own similar macro:

(defmacro with-api-connection [conn-sym arg1 arg2 & body]
  `(let [~conn-sym (API/getConnection ~arg1 ~arg2)]
     (try
      ~...@body
      (finally (API/closeConnection ~conn-sym)))))

So then you never forget to close the connection:

(try
 (with-api-connection conn ...
   (... do something with conn ...))
 (catch ...))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to