Almost! That should be macroexpand, not macroexpand-1, in case the macro expands to another macro (I have 'catch-cause' for catching a nested expression and then 'catch-psql' that utilises that).
(defmacro try [& body] (cons 'try (map macroexpand body))) Thanks again Crispin On Thursday, December 21, 2017 at 9:59:19 PM UTC+8, [email protected] wrote: > > Ah that explains it, Alex. > > What I ended up doing is making a custom try macro that macroexpands its > body forms and wraps them in a try, like this: > > (defmacro try* [& body] > (cons 'try (map macroexpand-1 body))) > > So by the time try is handled, the inner custom catch macros are already > expanded. > > Crispin > > On Thursday, December 21, 2017 at 12:36:22 AM UTC+8, Alex Miller wrote: >> >> `catch` is not a function or macro, it's special syntax only understood >> in the context of the special form `try`. You can't do this without writing >> your own form of try (see try+ in slingshot >> <https://github.com/scgilardi/slingshot> for an example of that). >> >> On Wednesday, December 20, 2017 at 9:53:40 AM UTC-6, >> [email protected] wrote: >>> >>> How do I use catch and throw in a macro? >>> >>> Here's a summary in the repl of what I have tried: >>> >>> user=> (defmacro catch2 [class bind-var & body] `(catch ~class ~bind-var >>> ~@body)) >>> #'user/catch2 >>> user=> (macroexpand-1 '(catch2 Exception e nil)) >>> (catch Exception e nil) >>> user=> (try nil (catch2 Exception e nil)) >>> >>> CompilerException java.lang.RuntimeException: Unable to resolve symbol: >>> catch in this context, >>> compiling:(/tmp/form-init7515078331866806804.clj:1:10) >>> >>> >>> Trying with explicit namespace on the catch: >>> >>> user=> (defmacro catch2 [class bind-var & body] `(clojure.core/catch >>> ~class ~bind-var ~@body)) >>> #'user/catch2 >>> user=> (macroexpand-1 '(catch2 Exception e nil)) >>> (clojure.core/catch Exception e nil) >>> user=> (try nil (catch2 Exception e nil)) >>> >>> CompilerException java.lang.RuntimeException: No such var: >>> clojure.core/catch, compiling:(/tmp/form-init7515078331866806804.clj:1:10) >>> >>> >>> I have tried with clojure 1.8.0 and 1.9.0. Both have the same outcome. >>> Can someone please point me to what I am doing wrong here, please? >>> >>> Thankyou >>> >>> Crispin >>> >> -- 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.
