This discussion is extended and subsumed under Error Handling Phase Next
Description
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
.
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 Svensson) - Why type based dispatch? Handlers are closed anyway. Maybe pattern matching? (Chris Houser)
- There's already a patch for supporting catching Exceptions inline with Conditions: assembla:80-add-catch-clauses-to-c-c-condition 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. (Phil Hagelberg)
- The patch referenced makes a difference between conditions and "normal" exceptions: for one you need "handle" clauses, for the others you need "catch" clauses. Also it does not allow a "finally" clause. Maybe the patch can be augmented? (Meikel Brandmeyer)