This discussion is extended and subsumed under Error Handling Phase Next


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.

Suggested improvements

Derive Condition from RuntimeException, not Throwable. (Laurent Petit)

Currently 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)

The proposed 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 try.

(try-case <dispatch-fn>
  (catch <dispatch-value> <local>

The original proposal used a multimethod inspired type based dispatch system. As modifications were suggested: