Should you close *out*?

195 Views Asked by At

Should you close *out* (standard output)? For example, via the with-open macro:

(require '[clojure.java.io :as io])

(with-open [w (io/writer *out*)]
  (.write w "foo"))

If I run this via Leiningen's (v2.7.0) lein run, I get an exception:

Exception in thread "main" java.io.IOException: Stream closed
    at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:45)
    at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:140)
    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
    at clojure.core$flush.invokeStatic(core.clj:3691)
    at clojure.main$main.invokeStatic(main.clj:423)
    at clojure.main$main.doInvoke(main.clj:386)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.lang.Var.applyTo(Var.java:700)
    at clojure.main.main(main.java:37)

Otherwise, the code works fine.

0

There are 0 best solutions below