处理 clojure 异常的更好方法以及为什么我的异常没有在 go 块中捕获
better way of handle clojure exceptions and why my exception is not captured inside go block
我试图找到一种更好的方法来处理 clojure 中的异常,我正在使用 https://github.com/alaisi/postgres.async which thrown an exeption when fail, but I would prefer return false (because I'm using a validator) or even better something like a Either monad (or something more simple like this http://adambard.com/blog/acceptable-error-handling-in-clojure/ )
1) 我试图捕获异常,如果存在 return false,但代码不起作用(不 return false 并抛出异常)。
(try
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid="
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid="
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?)
(catch Exception _ false))
2) 可以用一种方式换行吗 return 好吗?如果不起作用 return false,或者 [ok? nil] 和 [nil error] 可能是宏?
----感谢 swinn 我做了这个
;must receive an optional parameter for error response or
; build a [nil ok] response but just know this works for me...
(defmacro async-try-block! [block]
`(let [chn# (!/chan 1)]
(!/go
(let [response# (try
~block
(catch Throwable e#))]
(if (instance? Throwable response#) (!/put! chn# false) (!/put! chn# response#))))
chn#))
(async-try-block!
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid="
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid="
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?))
我不熟悉 postgres.async 库,但是 Exception
不是 JVM 中所有异常的根源,Throwable
是。
将捕获更改为 Throwable
将是我建议的第一个更改,但似乎 (<execute! ...)
实际上为您捕获了异常并且 returns 它,所以您需要使用 (instance? Throwable)
检查 return 值
我试图找到一种更好的方法来处理 clojure 中的异常,我正在使用 https://github.com/alaisi/postgres.async which thrown an exeption when fail, but I would prefer return false (because I'm using a validator) or even better something like a Either monad (or something more simple like this http://adambard.com/blog/acceptable-error-handling-in-clojure/ )
1) 我试图捕获异常,如果存在 return false,但代码不起作用(不 return false 并抛出异常)。
(try
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid="
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid="
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?)
(catch Exception _ false))
2) 可以用一种方式换行吗 return 好吗?如果不起作用 return false,或者 [ok? nil] 和 [nil error] 可能是宏?
----感谢 swinn 我做了这个
;must receive an optional parameter for error response or
; build a [nil ok] response but just know this works for me...
(defmacro async-try-block! [block]
`(let [chn# (!/chan 1)]
(!/go
(let [response# (try
~block
(catch Throwable e#))]
(if (instance? Throwable response#) (!/put! chn# false) (!/put! chn# response#))))
chn#))
(async-try-block!
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid="
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid="
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?))
我不熟悉 postgres.async 库,但是 Exception
不是 JVM 中所有异常的根源,Throwable
是。
将捕获更改为 Throwable
将是我建议的第一个更改,但似乎 (<execute! ...)
实际上为您捕获了异常并且 returns 它,所以您需要使用 (instance? Throwable)