Idiomatic error handling in Clojure

Emil Eriksson picture Emil Eriksson · Jan 2, 2015 · Viewed 7.7k times · Source

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?

Answer

Symfrog picture Symfrog · Jan 2, 2015

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.