Created
April 23, 2010 14:22
-
-
Save tristan/376581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?