try is in one macro, and catch is in the second, which is called first. How to make the following work?
(defmacro catch-me [] `(catch ~'Exception ~'ex true)) (defmacro try-me [] `(try (+ 4 3) (catch-me)))
The try-me extension looks good:
(clojure.walk/macroexpand-all '(try-me))
gives
(try (clojure.core/+ 4 3) (catch Exception ex true))
but a call (try-me) gives:
"Unable to resolve symbol: catch in this context",
which, BTW, is also the message you will receive in the REPL when using catch, if not in an attempt.
UPDATE:
Here is how I can make it work (thanks, @Barmar), here you can see the actual context of my code:
(defmacro try-me [& body] `(try ~@body ~@ (for [[e msg] [[com.mongodb.MongoException$Network "Database unreachable."] [com.mongodb.MongoException "Database problem."] [Exception "Unknown error."]]] `(catch ~e ~'ex (common/site-layout [:div {:id "errormessage"} [:p ~msg] [:p "Error is: " ~e] [:p "Message is " ~'ex]])))))
but this is what I was hoping for (using a separate catch-me macro):
(defmacro try-me [& body] `(try ~@body (catch-me com.mongodb.MongoException$Network "Database unreachable.") (catch-me com.mongodb.MongoException "Database problem.") (catch-me Exception "Unknown error.")))
I think it would be easier to write / support.
Any ideas? I need a citation syntax because I pass the parameters, so unfortunately Arthur's answer cannot be applied (or can it somehow?), But I did not publish my actual context just now.