Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Last active August 29, 2015 14:10

Revisions

  1. patrickgombert revised this gist Dec 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    ;
    ; (def illegal-argument IllegalArgumentException)
    ;
    ; (try (catch c _))
    ; (try (catch illegal-argument _))
    ;
    ; will raise the following exception:
    ; Unable to resolve classname: illegal-argument
  2. patrickgombert revised this gist Dec 11, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,15 @@
    ;
    ; (def illegal-argument IllegalArgumentException)
    ;
    ; (try (catch illegal-argument _))
    ; (try (catch c _))
    ;
    ; 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)))
    (= 'platform-catch (first item)))
    (rest item)
    acc))
    nil body)
    @@ -20,8 +20,9 @@
    catch-block (rest (rest c))
    try-block (take-while (fn [item]
    (if (seq? item)
    (not= (first item) :platform-catch)
    (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
  3. patrickgombert created this gist Dec 5, 2014.
    27 changes: 27 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    ; 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))))