Based on the discussion in this thread with Chouser, I've checked in code for "throwable maps with stack traces" at clojure.contrib.condition. It's a very generic scaffolding for flexible error handling. Here's Chouser's example implemented with clojure.contrib.condition:

-----------------

(ns clojure.contrib.condition.example
  (:use clojure.contrib.condition))

(defn func [x y]
  (if (neg? x)
    (raise :source ::Args :arg 'x :value x
      :message "shouldn't be negative")
    (+ x y)))

(defn main
  []
  (handler-case :source condition
    (println (func 3 4))
    (println (func -5 10))
    (handle ::Args
      (printf "Bad argument: %s\n" condition))))

-----------------

Clojure 1.1.0-alpha-SNAPSHOT
user=> (run clojure.contrib.condition.example)
7
Bad argument: {:stack-trace #<StackTraceElement[] [Ljava.lang.StackTraceElement;@6bf1a3f4>, :arg x, :message "shouldn't be negative", :source :clojure.contrib.condition.example/Args, :value -5}
nil
user=>

-----------------

Comments welcome.

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to