On Mon, Jan 24, 2011 at 4:07 PM, dysinger <t...@dysinger.net> wrote:
>
> (try (with-open [x y] ... )
>      (catch Exception))  ;; <- catches any exception from with-open macro
>
> I don't think you are correct.

He is. Close exceptions lose any other exception or return value:

user=> (defprotocol Closeable (close [this]))
Closeable
user=> (defprotocol Doable (doit [this]))
Doable
user=> (deftype Foo []
  Closeable
    (close [this] (throw (Exception. "close exception")))
  Doable
    (doit [this] (throw (Exception. "doit exception"))))
user.Foo
user=> (with-open [x (Foo.)] (doit x))
#<CompilerException java.lang.Exception: close exception (NO_SOURCE_FILE:242)>
user=> (try (with-open [x (Foo.)] (doit x)) (catch Exception e (.getMessage e)))
"close exception"

Note that the doit exception never escapes. To handle it you'd have to
catch it inside of the with-open. Even if you did, and returned a
sentinel value or threw a new exception, the close exception would
replace that.

(This also shows that deftype/defrecord and protocols can be used to
make custom types that can be closed by with-open.)

(I also note that deftype and defprotocol appear to depart from the
conventional behavior of deffoo macros of returning the Var that they
interned; they both seem to return the value that Var got, instead.)

-- 
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