annotate src/clojure/contrib/test_contrib/condition/example.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
rlm@10 2 ;; distribution terms for this software are covered by the Eclipse Public
rlm@10 3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
rlm@10 4 ;; be found in the file epl-v10.html at the root of this distribution. By
rlm@10 5 ;; using this software in any fashion, you are agreeing to be bound by the
rlm@10 6 ;; terms of this license. You must not remove this notice, or any other,
rlm@10 7 ;; from this software.
rlm@10 8 ;;
rlm@10 9 ;; clojure.contrib.condition.example.clj
rlm@10 10 ;;
rlm@10 11 ;; scgilardi (gmail)
rlm@10 12 ;; Created 09 June 2009
rlm@10 13
rlm@10 14 (ns clojure.contrib.condition.example
rlm@10 15 (:use (clojure.contrib
rlm@10 16 [condition
rlm@10 17 :only (handler-case print-stack-trace raise *condition*)])))
rlm@10 18
rlm@10 19 (defn func [x y]
rlm@10 20 "Raises an exception if x is negative"
rlm@10 21 (when (neg? x)
rlm@10 22 (raise :type :illegal-argument :arg 'x :value x))
rlm@10 23 (+ x y))
rlm@10 24
rlm@10 25 (defn main
rlm@10 26 []
rlm@10 27
rlm@10 28 ;; simple handler
rlm@10 29
rlm@10 30 (handler-case :type
rlm@10 31 (println (func 3 4))
rlm@10 32 (println (func -5 10))
rlm@10 33 (handle :illegal-argument
rlm@10 34 (print-stack-trace *condition*))
rlm@10 35 (println 3))
rlm@10 36
rlm@10 37 ;; multiple handlers
rlm@10 38
rlm@10 39 (handler-case :type
rlm@10 40 (println (func 4 1))
rlm@10 41 (println (func -3 22))
rlm@10 42 (handle :overflow
rlm@10 43 (print-stack-trace *condition*))
rlm@10 44 (handle :illegal-argument
rlm@10 45 (print-stack-trace *condition*)))
rlm@10 46
rlm@10 47 ;; nested handlers
rlm@10 48
rlm@10 49 (handler-case :type
rlm@10 50 (handler-case :type
rlm@10 51 nil
rlm@10 52 nil
rlm@10 53 (println 1)
rlm@10 54 (println 2)
rlm@10 55 (println 3)
rlm@10 56 (println (func 8 2))
rlm@10 57 (println (func -6 17))
rlm@10 58 ;; no handler for :illegal-argument
rlm@10 59 (handle :overflow
rlm@10 60 (println "nested")
rlm@10 61 (print-stack-trace *condition*)))
rlm@10 62 (println (func 3 4))
rlm@10 63 (println (func -5 10))
rlm@10 64 (handle :illegal-argument
rlm@10 65 (println "outer")
rlm@10 66 (print-stack-trace *condition*))))