异常处理
Clojure代码里面抛出来的异常都是运行时异常。当然从Clojure代码里面调用的java代码还是可能抛出那种需要检查的异常的。
try - catch - finally 以及throw 提供了和java里面类似的功能。
try、catch、throw、finally:
- user=>(try(throw(Exception."error"))(finally(println"final")))
- final
- Exceptionerroruser/eval310(NO_SOURCE_FILE:1)
- user=>(try(/30)(catchExceptione(printlne)))
- #<ArithmeticExceptionjava.lang.ArithmeticException:Dividebyzero>
- nil
assert:它测试一个表达式, 如果这个表达式的值为false的话,它会抛出异常。
- user=>(asserttrue)
- nil
- user=>(assertfalse)
- AssertionErrorAssertfailed:falseuser/eval317(NO_SOURCE_FILE:1)
- user=>(assertnil)
- AssertionErrorAssertfailed:niluser/eval319(NO_SOURCE_FILE:1)
- user=>(assert0)
- nil
- user=>(assert[123])
- nil
- user=>(assert"foo")
- nil
clojure 完整示例:
(ns helloclojure.myexception)
(defn exception_test1 []
(try (throw (Exception. "--error--"))
(finally (println "final"))
))
(defn exception_test2 []
(try (/ 3 0)
(catch Exception e (println "error: " e))
))
(defn exception_test3 [cls]
(try (Class/forName cls) true
(catch ClassNotFoundException e false))
)
;(exception_test1)
(exception_test2) ; error: #<ArithmeticException java.lang.ArithmeticException: Divide by zero>
;(exception_test3 helloclojure.myexception)
(assert true)
;(assert false)
;(try (assert false)
; (catch Exception e (println "error" e))
; (finally (println 'finally)))
;(assert nil)
(assert [1 2 3])
(assert "foo")
(assert 0)
(print 'end)
分享到:
相关推荐
NULL 博文链接:https://clojure.iteye.com/blog/1741375
NULL 博文链接:https://clojure.iteye.com/blog/1742475
Clojure入门教程
Clojure学习教程.pdf
Clojure入门教程- Clojure – Functional Programming for the JVM中文版
Clojure入门介绍: Clojure - Functional Programming for the JVM
Clojure学习文档,Clojure-Web开发学习,覆盖了Clojure由浅入深的基本学习
本次演讲中,庄晓丹从AVOSCloud这个完全基于Clojure构建的移动云平台出发,分享Avos团队在Clojure上的实践和经验,为国内Clojure的传播贡献一份力量。
这是Programming Clojure 电子版的 纸质版本在美国亚马逊要到2009年3月才能上架 Paperback: 200 pages Publisher: Pragmatic Bookshelf (March 15, 2009) Language: English ISBN-10: 1934356336 ISBN-13: 978-...
[Packt Publishing] Clojure 数据分析 经典实例 (英文版) [Packt Publishing] Clojure Data Analysis Cookbook (E-Book) ☆ 出版信息:☆ [作者信息] Eric Rochester [出版机构] Packt Publishing [出版日期] ...
Clojure is an opinionated language—it doesn’t try to cover all paradigms or provide every checklist bullet-point feature. Instead it provides the features needed to solve all kinds of real-world ...
Practical Clojure Clojure语言书籍
clojure clojure clojureclojure clojure
Clear, practical Clojure for the professional programmer Professional Clojure is the experienced developer's guide to functional programming using the Clojure language. Designed specifically to meet ...
Titanium 是一个强大的 Clojure 图形库,基于 Aurelius Titan 构建,提供了 Clojure 风格的 API 和图像处理 DSL 。 标签:Clojure
【1】[Clojure编程乐趣](The Joy of Clojure).pdf 【2】Clojure – Functional Programming for the JVM中文版.pdf 【3】Clojure Cookbook.pdf 【4】Clojure Data Analysis Cookbook.pdf 【5】clojure Hand book...
[2013] Functional Programming Patterns in Scala and Clojure - Write Lean Programs for the JVM.(Michael Bevilacqua-Linn).[1937785475].pdf+epub.rar [2014] Clojure Cookbook - Recipes for Functional ...
Typed Clojure 保留了 Clojure 的优势,是 Clojure 的可选类型系统,也可以说是 Clojure 的一个库,改善了大量的静态类型安全检测。主要特性:从 Java 中保护你的 Clojure 程序,进行安全的互操作,正确的使用外部 ...