异常

处理异常

try/catch/finally来处理异常

user=> (try
  #=>   (/ 1 0)
  #=>   (catch Exception e
  #=>    (println "/ zero!!"))
  #=>    (finally
  #_=>      (println "finish!")))
/ zero!!
finish!
nil

抛出异常

throw抛出异常

(throw (Exception "something wrong !"))

异常数据

  • ex-info 携带一个字符串和一个map
  • ex-data 可以获得map,如果map没有设置则返回nil
 (try
  (throw (ex-info "There was a problem" {:detail 42}))
  (catch Exception e
    (prn (:detail (ex-data e)))))