Notice it says "can't recur from within a catch". So throw the try/catch into it's own function and return nil if the try fails. Then throw a if statement to continue the loop only if the function return nil.
Timothy On Thu, Mar 17, 2011 at 8:49 PM, Fiel Cabral <e4696wyoa63emq6w3250kiw60i4...@gmail.com> wrote: > Hello Clojure users, > This is a dumb question but I'd like to write something equivalent to this > in Clojure: > > public String loop_with_exception(int retries) > { > for (int n = retries; n > 0; n--) { > try { > return some_io_operation(); > } catch (IOException e) { > continue; > } > } > return null; > } > So I tried writing it like this: > > (ns sandbox.core > (:import [java.io.IOException])) > > (defn some-io-operation > "Some read I/O operation that could throw an IOException." > [] > (println "WOULD do a read operation")) > (defn loop-with-exception [retries] > (loop [n retries] > (try > (when (pos? n) > (some-io-operation)) > (catch IOException e > (recur (dec n)))))) > > But I get this error: > cd c:/EMACSHOME/CLOJURE-PROJECTS/sandbox/src/sandbox/ > 1 compiler notes: > Unknown location: > error: java.lang.UnsupportedOperationException: Cannot recur from > catch/finally > core.clj:13:9: > error: java.lang.UnsupportedOperationException: Cannot recur from > catch/finally (core.clj:13) > Compilation failed. > What's the recommended way to handle exceptions and retry inside a > loop/recur? > Thank you. > -Fiel Cab.ral > > -- > 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 -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- 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