rlm@10: ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and rlm@10: ;; distribution terms for this software are covered by the Eclipse Public rlm@10: ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can rlm@10: ;; be found in the file epl-v10.html at the root of this distribution. By rlm@10: ;; using this software in any fashion, you are agreeing to be bound by the rlm@10: ;; terms of this license. You must not remove this notice, or any other, rlm@10: ;; from this software. rlm@10: ;; rlm@10: ;; clojure.contrib.condition.example.clj rlm@10: ;; rlm@10: ;; scgilardi (gmail) rlm@10: ;; Created 09 June 2009 rlm@10: rlm@10: (ns clojure.contrib.condition.example rlm@10: (:use (clojure.contrib rlm@10: [condition rlm@10: :only (handler-case print-stack-trace raise *condition*)]))) rlm@10: rlm@10: (defn func [x y] rlm@10: "Raises an exception if x is negative" rlm@10: (when (neg? x) rlm@10: (raise :type :illegal-argument :arg 'x :value x)) rlm@10: (+ x y)) rlm@10: rlm@10: (defn main rlm@10: [] rlm@10: rlm@10: ;; simple handler rlm@10: rlm@10: (handler-case :type rlm@10: (println (func 3 4)) rlm@10: (println (func -5 10)) rlm@10: (handle :illegal-argument rlm@10: (print-stack-trace *condition*)) rlm@10: (println 3)) rlm@10: rlm@10: ;; multiple handlers rlm@10: rlm@10: (handler-case :type rlm@10: (println (func 4 1)) rlm@10: (println (func -3 22)) rlm@10: (handle :overflow rlm@10: (print-stack-trace *condition*)) rlm@10: (handle :illegal-argument rlm@10: (print-stack-trace *condition*))) rlm@10: rlm@10: ;; nested handlers rlm@10: rlm@10: (handler-case :type rlm@10: (handler-case :type rlm@10: nil rlm@10: nil rlm@10: (println 1) rlm@10: (println 2) rlm@10: (println 3) rlm@10: (println (func 8 2)) rlm@10: (println (func -6 17)) rlm@10: ;; no handler for :illegal-argument rlm@10: (handle :overflow rlm@10: (println "nested") rlm@10: (print-stack-trace *condition*))) rlm@10: (println (func 3 4)) rlm@10: (println (func -5 10)) rlm@10: (handle :illegal-argument rlm@10: (println "outer") rlm@10: (print-stack-trace *condition*))))