异常处理
Clojure代码里面抛出来的异常都是运行时异常。当然从Clojure代码里面调用的java代码还是可能抛出那种需要检查的异常的。 try
, catch
, finally
以及 throw
提供了和java里面类似的功能:
(defn collection? [obj]
(println "obj is a" (class obj))
; Clojure collections implement clojure.lang.IPersistentCollection.
(or (coll? obj) ; Clojure collection?
(instance? java.util.Collection obj))) ; Java collection?
(defn average [coll]
(when-not (collection? coll)
(throw (IllegalArgumentException. "expected a collection")))
(when (empty? coll)
(throw (IllegalArgumentException. "collection is empty")))
; Apply the + function to all the items in coll,
; then divide by the number of items in it.
(let [sum (apply + coll)]
(/ sum (count coll))))
(try
(println "list average =" (average '(2 3))) ; result is a clojure.lang.Ratio object
(println "vector average =" (average [2 3])) ; same
(println "set average =" (average #{2 3})) ; same
(let [al (java.util.ArrayList.)]
(doto al (.add 2) (.add 3))
(println "ArrayList average =" (average al))) ; same
(println "string average =" (average "1 2 3 4")) ; illegal argument
(catch IllegalArgumentException e
(println e)
;(.printStackTrace e) ; if a stack trace is desired
)
(finally
(println "in finally")))
上面代码的输出是这样的:
obj is a clojure.lang.PersistentList
list average = 5/2
obj is a clojure.lang.LazilyPersistentVector
vector average = 5/2
obj is a clojure.lang.PersistentHashSet
set average = 5/2
obj is a java.util.ArrayList
ArrayList average = 5/2
obj is a java.lang.String
#<IllegalArgumentException java.lang.IllegalArgumentException:
expected a collection>
in finally