I wish to throw an exception and have the following:

(throw "Some text")

However it seems to be ignored.


You need to wrap your string in a Throwable:

(throw (Throwable. "Some text"))

or

(throw (Exception. "Some text"))

You can set up a try/catch/finally block as well:

(defn myDivision [x y]
  (try
    (/ x y)
    (catch ArithmeticException e
      (println "Exception message: " (.getMessage e)))
    (finally 
      (println "Done."))))

REPL session:

user=> (myDivision 4 2)
Done.
2
user=> (myDivision 4 0)
Exception message:  Divide by zero
Done.
nil

clojure.contrib.condition provides a Clojure-friendly means of handling exceptions. You can raise conditons with causes. Each condition can have its own handler.

There are a number of examples in the source on github.

It's quite flexible, in that you can provide your own key, value pairs when raising and then decide what to do in your handler based on the keys/values.

E.g. (mangling the example code):

(if (something-wrong x)
  (raise :type :something-is-wrong :arg 'x :value x))

You can then have a handler for :something-is-wrong:

(handler-case :type
  (do-non-error-condition-stuff)
  (handle :something-is-wrong
    (print-stack-trace *condition*)))