lazy-seq is the short answer.
You are constructing the seq inside the try/catch but it is realized
outside of that so no exceptions will be caught.
(defn gen-ym-list [from to]
(try
(->> (p/periodic-seq (f/parse ym-fmt from) (t/months 1))
(take-while #(not (t/after? % (f/parse ym-fmt to))))
(into [])
(catch Exception _ nil)))
This forces the seq inside the try/catch.
HTH,
Thomas
On Wednesday, February 14, 2018 at 11:46:58 AM UTC+1, icamts wrote:
>
> Hi all,
> I found an unexpected behavior of (try ... (catch ...)) special form. Can
> someone help me understand it better?
>
> These are project.clj and core.clj of my test project. The test project
> zip file is provided as an attachment.
>
> (defproject try-try "0.0.0"
> :description "unexpected try beahvior"
> :url "http://example.com/FIXME"
> :license {:name "Eclipse Public License"
> :url "http://www.eclipse.org/legal/epl-v10.html"}
> :dependencies [[org.clojure/clojure "1.8.0"]
> [org.clojure/tools.cli "0.3.5"]
> [clj-time "0.14.0"]
> [joda-time/joda-time "2.9.7"]])
>
> (ns try-try.core
> (:require [clj-time.core :as t]
> [clj-time.format :as f]
> [clj-time.periodic :as p]))
>
> (def ym-fmt (f/formatter "yyyyMM"))
>
> (defn gen-ym-list [from to]
> (try
> (->> (p/periodic-seq (f/parse ym-fmt from) (t/months 1))
> (take-while #(not (t/after? % (f/parse ym-fmt to)))))
> (catch Exception _ nil)))
>
> (defn gen-ym-list* [from to]
> (try
> (let [ym-from (f/parse ym-fmt from)
> ym-to (f/parse ym-fmt to)]
> (->> (p/periodic-seq ym-from (t/months 1))
> (take-while #(not (t/after? % ym-to)))))
> (catch Exception _ nil)))
>
> (gen-ym-list "2017" "201802"); returns nil as expected
>
> (gen-ym-list "201712" "2018"); throws unexpected exception
>
> (gen-ym-list* "2017" "201802"); returns nil as expected
>
> (gen-ym-list* "201712" "2018"); returns nil as expected
>
> (use 'clojure.walk)
>
> (macroexpand-all
> `(try
> (->> (p/periodic-seq (f/parse ym-fmt from) (t/months 1))
> (take-while #(not (t/after? % (f/parse ym-fmt to)))))
> (catch Exception _ nil)))
>
> (macroexpand-all
> `(try
> (let [date-from (f/parse ym-fmt from)
> date-to (f/parse ym-fmt to)]
> (->> (p/periodic-seq date-from (t/months 1))
> (take-while #(not (t/after? % date-to)))))
> (catch Exception _ nil)))
>
> gen-ym-list doesn't catch IllegalArgumentException thrown when to
> argument is malformed. It catches the same exception thrown when from is
> malformed.
>
> gen-ym-list* with arguments parsing in an initial let form works as
> expected.
>
> From macro expansion I can see the to is parsed inside a fn*. Is this a
> known limit in try-catch special form? Or a bug? Thanks in advance for your
> help.
>
> Cheers,
> Luca
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.