Idiomatic error handling in Clojure
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.
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.