Skip to content

Instantly share code, notes, and snippets.

@tristan
Created April 23, 2010 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristan/376581 to your computer and use it in GitHub Desktop.
Save tristan/376581 to your computer and use it in GitHub Desktop.
(defn dostuff [x]
(try
(cond (vector? x)
(let [r (for [x x] (dostuff x))]
(apply concat r))
(integer? x)
(throw (java.text.ParseException. "blah" 0))
(string? x)
(throw (NoSuchMethodException. x))
:else
[x])
(catch Throwable e
(if (and (vector? x) (some #{:catch-error} x) (not (= (class e) java.text.ParseException)))
[]
(throw e)))))
(println
(dostuff [:a :b [:c :d :e [:catch-error :f :g [:h :j ["abc"]]]]])
)
(println
(dostuff [:a :b [:c :d :e [:catch-error :f :g [:h :j [1]]]]])
)
; one solution
(require 'clojure.stacktrace)
(defn dostuff [x]
(try
(cond (vector? x)
(let [r (for [x x] (dostuff x))]
(apply concat r))
(integer? x)
(throw (java.text.ParseException. "blah" 0))
(string? x)
(throw (NoSuchMethodException. x))
:else
[x])
(catch Exception e
(if (and (vector? x) (some #{:catch-error} x) (not (= (class (clojure.stacktrace/root-cause e)) java.text.ParseException)))
[]
(throw e)))))
(println
(dostuff [:a :b [:c :d :e [:catch-error :f :g [:h :j ["abc"]]]]])
)
(try
(println
(dostuff [:a :b [:c :d :e [:catch-error :f :g [:h :j [1]]]]])
)
(catch Exception e
(println (clojure.stacktrace/root-cause e))))
@tristan
Copy link
Author

tristan commented Apr 23, 2010

basically, if the recursive function finds something it doesn't like it'll throw an exception. if the exception is not a ParseException and a :catch-error element is present in the input, it will handle the error otherwise it throws it down to the stack which checks the same thing.
The problem is that the exception gets wrapped in RuntimeExceptions as it goes down the stack so the = class call stops working after the initial throw. anyone got any ideas on how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment