Last active
August 29, 2015 14:08
-
-
Save yayitswei/d80d51ee29dc5e2c1117 to your computer and use it in GitHub Desktop.
Unexpected behavior using try/catch in a go block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns queue-test | |
(:require [clojure.core.async :as async :refer [>! <! chan go go-loop put!]])) | |
(defn process! [item] | |
{:pre [item]} | |
(println "processing:" item)) | |
(defn init! [] | |
(let [q (chan)] | |
(go-loop [] | |
(try (process! (<! q)) | |
(catch Exception e | |
(.printStackTrace e))) | |
(recur)) | |
q)) | |
(defn test! [] | |
(let [q (init!)] | |
;; the first queue fails the preconditions for process! | |
(put! q false) | |
;; I expect this one to work, but it doesn't in practice | |
(put! q true))) | |
(defn test2! [] | |
(let [q (init!)] | |
;; without the first failing put!, this works as expected | |
(put! q true))) |
should catch Throwable instead of Exception
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've just added two tests and a bit more explanation.
test!
fails andtest2!
works as expected.Also swapped out
(go (<! ..))
forput!
, per your suggestion.