try* macro to catch multiple exception classes with one body. feedback is needed.

361 views
Skip to first unread message

Max Gonzih

unread,
Jun 19, 2013, 11:00:17 AM6/19/13
to clo...@googlegroups.com
Hi, I implemented small macro to catch multiple exception classes with one body.

https://gist.github.com/Gonzih/5814945

What do you think? Are there better ways to achieve similar results?

Thanks!

Meikel Brandmeyer (kotarak)

unread,
Jun 20, 2013, 1:47:37 AM6/20/13
to clo...@googlegroups.com
Hi,
I would just extend try a simply as possible: simply add the catch-all, but keep catch and finally as is. Here my try:

    (defmacro try*
      [& body]
      (let [catch-all?   #(and (seq? %) (= (first %) 'catch-all))
            expand-catch (fn [[_catch-all exceptions & catch-tail]]
                           (map #(list* 'catch % catch-tail) exceptions))
            transform    (fn [form]
                           (if (catch-all? form)
                             (expand-catch form)
                             [form]))]
        (cons `try (mapcat transform body))))

    (try*
      (println :a)
      (println :b)
      (catch-all [A B C] e (println (type e)))
      (catch D _ (println "Got D!"))
      (finally (println "Finally!")))

expands to

    (try
      (println :a)
      (println :b)
      (catch A e (println (type e)))
      (catch B e (println (type e)))
      (catch C e (println (type e)))
      (catch D _ (println "Got D!"))
      (finally (println "Finally!")))

Kind regards
Meikel

Max Gonzih

unread,
Jun 20, 2013, 2:57:57 AM6/20/13
to clo...@googlegroups.com

Right, simpler solution, thanks.

Regards,
Max

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/0Suarc57WCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Max Gonzih

unread,
Jun 20, 2013, 3:25:12 AM6/20/13
to clo...@googlegroups.com
I updated my macro to your solution, looks really simple and works like before. I don't know why I overcomplicated my original solution so much :).

Thanks again!

Herwig Hochleitner

unread,
Jun 20, 2013, 4:26:13 PM6/20/13
to clo...@googlegroups.com
One thing to consider: try* is a compiler builtin form. Those are currently not even namespaced. That might lead to confusion for readers.


2013/6/20 Max Gonzih <gon...@gmail.com>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
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 clojure+u...@googlegroups.com.

Max Gonzih

unread,
Jun 22, 2013, 11:42:20 AM6/22/13
to clo...@googlegroups.com

I can't think about another name for my macro :). Any ideas?

Regards,
Max

You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/0Suarc57WCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages