Currently it is not easy to create custom exceptions in Clojure. This requires
gen-class and thus AOT compilation with all its drawbacks. One approach to improve the situation was
clojure.contrib.condition. It defines a custom exception –
Condition – which may be equipped with arbitrary metadata. This makes it quite easy to handle errors with arbitrary context information without having to define custom errors.
Throwable. (Laurent Petit)
handler-case is limited to handle
Conditions. However Clojure embraces the host platform. And this includes exceptions. So it should be easy to handle
Conditions and host exceptions in the same form. (Meikel Brandmeyer)
try-case (name tbd.) closely mimics the built-in
try special form. However it also takes a dispatch function. Upon catching a
Throwable it is fed to the dispatch function. Its result is compared to the dispatch value of the
catch clauses. The body of the first matching clause is executed. If no clause matches, the exception is re-thrown.
finally works as with plain
The original proposal used a multimethod inspired type based dispatch system. As modifications were suggested:
- Use the class of the throwable as dispatch value in case the dispatch function returns
nil. (Rasmus Svendsson)
- Why type based dispatch? Handlers are closed anyway. Maybe pattern matching? (Chris Houser)
- Actually handler-case already supports catching Exceptions along with Conditions. You can include (catch Exception e ...) clauses interspersed with your (handle :foo ...) clauses in the current implementation. The only thing that's missing is documentation.