Idiomatic error handling in Clojure

2019-03-14 08:18发布

When I put on my C hat, I think that maybe idiomatic Clojure just does the simple thing and checks return values.

When I put on my Java hat (reluctantly, I must add), I think to myself that since Clojure runs on the JVM the natural way must be to use JVM exceptions.

When I put on my functional hat, I'm thinking that there must be some sort of monadic construction or threading macro that can handle errors in a composable way.

So what's the idiomatic way to handle errors in a Clojure program?

3条回答
霸刀☆藐视天下
2楼-- · 2019-03-14 08:34

For a very functional approach, have a look at cats, which would correspond to "some kind of monadic construction" :

Category Theory and Algebraic abstractions for Clojure and ClojureScript. http://funcool.github.io/cats/latest/

Example taken from their documentation :

(require '[cats.core :as m])
(require '[cats.monad.maybe :as maybe])

(m/mappend (maybe/just [1 2 3])
           (maybe/nothing)
           (maybe/just [4 5 6])
           (maybe/nothing))

You can see that nothing is somewhat equivalent to nil, except you don't have to check anything manually.

查看更多
成全新的幸福
3楼-- · 2019-03-14 08:41

Clojure error handling is generally JVM exception (unchecked) oriented.

Slingshot makes using exceptions more pleasant by allowing, for example, destructuring on thrown exception values.

For an alternative that allows erlang-style error handling you should look at dire. This blog post gives a good overview of the rational for dire as well as an overview of Clojure error handling mechanisms and drawbacks.

查看更多
不美不萌又怎样
4楼-- · 2019-03-14 08:46

The official means for error handling in Clojure is exceptions, since it comes with the JVM. However, functions are not referentially transparent when they throw exceptions and this property is not composable either. Exceptions are not functional.

Many people have tried out alternative/monadic error handling in different styles in Clojure. I have created Promenade https://github.com/kumarshantanu/promenade for this purpose and have had good experience using it.

查看更多
登录 后发表回答