diff options
author | scgilardi <scgilardi@gmail.com> | 2009-06-16 04:12:47 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-06-16 04:12:47 +0000 |
commit | 443409f56420f3dc7972131c34c189e63d28fdcb (patch) | |
tree | 02fb2a9a12639f8462d43ae7d68fc74d697eba0c /src/clojure/contrib/condition | |
parent | a69831f5c84f23f5159448fe2ec0acb633cacf7c (diff) |
condition: refinements
Diffstat (limited to 'src/clojure/contrib/condition')
-rw-r--r-- | src/clojure/contrib/condition/Condition.clj | 26 | ||||
-rw-r--r-- | src/clojure/contrib/condition/example.clj | 21 |
2 files changed, 31 insertions, 16 deletions
diff --git a/src/clojure/contrib/condition/Condition.clj b/src/clojure/contrib/condition/Condition.clj index b3a24340..18449653 100644 --- a/src/clojure/contrib/condition/Condition.clj +++ b/src/clojure/contrib/condition/Condition.clj @@ -16,16 +16,28 @@ (ns clojure.contrib.condition.Condition (:gen-class :extends Throwable :implements [clojure.lang.IMeta] - :state _meta + :state state :init init - :constructors - {[clojure.lang.IPersistentMap] []})) + :post-init post-init + :constructors {[clojure.lang.IPersistentMap] + [String Throwable]})) (defn -init - [meta] - [[] meta]) + "Constructs a Condition object with condition (a map) as its + metadata. Also initializes the superclass with the values at :message + and :cause, if any, so they are also available via .getMessage and + .getCause." + [condition] + [[(:message condition) (:cause condition)] (atom condition)]) + +(defn -post-init + "Adds :stack-trace to the condition. Drops the bottom 3 frames because + they are always the same: implementation details of Condition and raise." + [this condition] + (swap! (.state this) assoc + :stack-trace (into-array (drop 3 (.getStackTrace this))))) (defn -meta + "Returns this object's metadata, the condition" [this] - (assoc (._meta this) - :stack-trace (into-array (drop 3 (.getStackTrace this))))) + @(.state this)) diff --git a/src/clojure/contrib/condition/example.clj b/src/clojure/contrib/condition/example.clj index 36d7b36f..5a7d72ef 100644 --- a/src/clojure/contrib/condition/example.clj +++ b/src/clojure/contrib/condition/example.clj @@ -12,28 +12,31 @@ ;; Created 09 June 2009 (ns clojure.contrib.condition.example - (:use clojure.contrib.condition)) + (:use (clojure.contrib + [condition + :only (handler-case print-stack-trace raise *condition*)]))) (defn func [x y] - (if (neg? x) - (raise :reason :illegal-argument :arg 'x :value x :message "cannot be negative") - (+ x y))) + "Raises an exception if x is negative" + (when (neg? x) + (raise :type :illegal-argument :arg 'x :value x)) + (+ x y)) (defn main [] ;; simple handler - (handler-case :reason + (handler-case :type (println (func 3 4)) (println (func -5 10)) (handle :illegal-argument - (print-stack-trace *condition*)) + (print-stack-trace *condition*)) (println 3)) ;; multiple handlers - (handler-case :reason + (handler-case :type (println (func 4 1)) (println (func -3 22)) (handle :overflow @@ -43,8 +46,8 @@ ;; nested handlers - (handler-case :reason - (handler-case :reason + (handler-case :type + (handler-case :type nil nil (println 1) |