Exception handling 试试弹弓/试试+;差异?

Exception handling 试试弹弓/试试+;差异?,exception-handling,clojure,Exception Handling,Clojure,我试图为cljhttp返回404时编写一个异常处理程序。根据中的例外部分: CLJHTTP将抛出一个弹弓石,可以被普通玩家捕获 (捕捉异常e…)或在弹弓的try+块中 试着这样做,似乎有一些不同之处我很难弄清楚: (ns my-app.core (:require [clj-http.client :as client]) (:use [slingshot.slingshot])) (try (client/get "http://some-site.com/broken")

我试图为
cljhttp
返回404时编写一个异常处理程序。根据中的例外部分:

CLJHTTP将抛出一个弹弓石,可以被普通玩家捕获 (捕捉异常e…)或在弹弓的try+块中

试着这样做,似乎有一些不同之处我很难弄清楚:

(ns my-app.core
  (:require [clj-http.client :as client])
  (:use [slingshot.slingshot]))

(try
  (client/get "http://some-site.com/broken")
  (catch Exception e (print "I found a problem!")))

=> I found a problem!
   nil

(try+
  (client/get "http://some-site.com/broken")
  (catch Exception e (print "I found a problem!")))

=> ExceptionInfo clj-http: status 404  clj-http.client/wrap-exceptions/fn--1604 (client.clj:147)

如果不筛选
Exception
的子类,则可以:

(try+
  (client/get "http://some-site.com/broken")
  (catch Object _ (print "I found a problem!")))
这里抛出的是一个带有异常元数据的本地Clojure映射,而不是传统的Java异常

因此,如果您使用的是Slingshot,那么您可以在该映射上进行过滤——使用请求状态、返回的内容、重定向等字段——以使除块具有比类更细粒度的选择。例如:

(try+
  (client/get "http://some-site.com/broken")
  (catch [:status 404] {:keys [trace-redirects]}
    (print "No longer exists! (redirected through " trace-redirects ")")))