Well... for a start I suggest you read up on clojure's macro syntax. I'll provide a bit of a primmer here but I'm not going to go into depth.
First things first, here's your macro.
(defmacro safe [bindings? & forms]
(let [bindings (if (and (even? (count bindings?)) (vector? bindings?))
bindings? nil)
forms (if bindings forms (cons bindings? forms))
except `(catch Exception e# e#)]
(if bindings
`(let ~bindings (try ~@forms ~except))
`(try ~@forms ~except))))
And now for a walk through.
Clojure's (let) macro demands a vector with an even number of arguments and supports some really interesting behavior called destructuring. For the purposes of this macro, I assume that any valid binding argument will first be a vector and second be of even length. The evaluation of (let) will perform this same check, but this macro must do it as it's possible that the first form is not a binding but a form to be evaluated and should exhibit different behavior in that case.
As to the macro itself, I use a (let) to process the arguments, the symbol bindings
serving the double purpose of indicating the presence of bindings as well as taking the binding vector if one is present. forms
is re-defined from its initial binding in the arguments (clojure lets you do that) to a value which is impacted by that of bindings
being the entire form sequence which you wish to execute in an error-contained environment. The except
symbol really isn't called for, it's just to escape the code duplication of restating that (catch) form in each of the expansion cases.
The symbol ` (known as backquote or backtick) which I use is equivalent here to normal quote (') except that clojure allows me to use the macro expansion syntax within backquoted forms and not quoted forms. The macro syntax contains the ~ (unquote) operator and the ~@ (insert (unquote)) uperator. Using these three bits of notation I've defined both desired cases, the let with a binding form where I insert the binding form and the forms to be tried and the simple try only case.
The conditional could be eliminated to produce
(defmacro safe [bindings? & forms]
(let [bindings (if (and (even? (count bindings?)) (vector? bindings?))
bindings? [])
forms (if-not (empty? bindings)
forms (cons bindings? forms))
except `(catch Exception e# e#)]
`(let ~bindings (try ~@forms ~except))))
but then you have a superfluous (let) when there is no binding form.