Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Last active August 29, 2015 14:10
Show Gist options
  • Save patrickgombert/278b9ea067ebf772683a to your computer and use it in GitHub Desktop.
Save patrickgombert/278b9ea067ebf772683a to your computer and use it in GitHub Desktop.
allow def'd classes to be used in try...catch
; try is a special form that seems to not allow def'd classes to be used
; for example:
;
; (def illegal-argument IllegalArgumentException)
;
; (try (catch illegal-argument _))
;
; will raise the following exception:
; Unable to resolve classname: illegal-argument
(defmacro platform-try [& body]
(let [c (reduce (fn [acc item]
(if (and (seq? item)
(= 'platform-catch (first item)))
(rest item)
acc))
nil body)
exception-class (eval (first c))
exception-var (first (rest c))
catch-block (rest (rest c))
try-block (take-while (fn [item]
(if (seq? item)
(not= (first item) 'platform-catch)
true)) body)]
`(try ~@try-block
(catch ~exception-class ~exception-var ~@catch-block))))
(playform-try (throw (IllegalArgumentException.)) (platform-catch illegal-argument _ nil)) ; => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment