• (handler-case dispatch-fn & body)
Executes body in a context where raised conditions can be handled.

dispatch-fn accepts a raised condition (a map) and returns a selector
used to choose a handler. Commonly, dispatch-fn will be :type to dispatch
on the condition's :type value.

Handlers are forms within body:

(handle key
...)

If a condition is raised, executes the body of the first handler whose
key satisfies (isa? selector key). If no handlers match, re-raises the
condition.

While a handler is running, *condition* is bound to the condition being
handled and *selector* is bound to to the value returned by dispatch-fn
that matched the handler's key.

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/condition.clj:71 top

(defmacro handler-case
  "Executes body in a context where raised conditions can be handled.

  dispatch-fn accepts a raised condition (a map) and returns a selector
  used to choose a handler. Commonly, dispatch-fn will be :type to dispatch
  on the condition's :type value.

  Handlers are forms within body:

    (handle key
      ...)

  If a condition is raised, executes the body of the first handler whose
  key satisfies (isa? selector key). If no handlers match, re-raises the
  condition.

  While a handler is running, *condition* is bound to the condition being
  handled and *selector* is bound to to the value returned by dispatch-fn
  that matched the handler's key."
  [dispatch-fn & body]
  (let [[handlers code]
        (separate #(and (list? %) (= 'handle (first %))) body)]
    `(try
      ~@code
      (catch Condition c#
        (binding [*condition-object* c#
                  *condition* (meta c#)
                  *selector* (~dispatch-fn (meta c#))]
          (cond
           ~@(mapcat
              (fn [[_ key & body]]
                `[(isa? *selector* ~key) (do ~@body)])
              handlers)
           :else (raise)))))))
Vars in clojure.contrib.condition/handler-case: defmacro fn key let mapcat apply binding concat cond isa? list meta seq vector
Used in 0 other vars

Comments top

No comments for handler-case. Log in to add a comment.