作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为clj-http
返回404时编写一个异常处理程序。根据documentation中的Exceptions部分:
clj-http will throw a Slingshot Stone that can be caught by a regular (catch Exception e ...) or in Slingshot's try+ block
(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!")))
(try+
(client/get "http://some-site.com/broken")
(catch [:status 404] {:keys [trace-redirects]}
(print "No longer exists! (redirected through " trace-redirects ")")))
关于exception-handling - 尝试和弹弓/尝试+的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708580/
我是一名优秀的程序员,十分优秀!