1.3.0 permalink Arrow_down_16x16

set-error-handler!

clojure.core

  • (set-error-handler! a handler-fn)
Sets the error-handler of agent a to handler-fn. If an action
being run by the agent throws an exception or doesn't pass the
validator fn, handler-fn will be called with two arguments: the
agent and the exception.

1 Example top

  • (def bond (agent 7))
    
    (defn err-handler-fn [ag ex]
      (println "evil error occured: " ex " and we still have value " @ag))
    
    (set-error-handler! bond err-handler-fn)
    
    ;;division by zero:
    
    (send bond (fn [x] (/ x 0)))
    =>evil error occured:  #<ArithmeticException java.lang.ArithmeticException: 
    =>Divide by zero>  and we still have value  7
    
    (send bond inc)
    =>FAILURE ;;Agent is failed, needs restart, but keeps the last OK value
    
    @bond
    =>7
    
    (restart-agent bond 7) ;; or replace 7 with @ag
    
    (send bond inc)
    =>#<Agent@88d00c6: 7> ;;because of async update
    
    @bond
    =>8
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1961 top

(defn set-error-handler!
  "Sets the error-handler of agent a to handler-fn.  If an action
  being run by the agent throws an exception or doesn't pass the
  validator fn, handler-fn will be called with two arguments: the
  agent and the exception."
  {:added "1.2"
   :static true}
  [^clojure.lang.Agent a, handler-fn]
  (.setErrorHandler a handler-fn))
Vars in clojure.core/set-error-handler!:
Used in 0 other vars

Comments top

No comments for set-error-handler!. Log in to add a comment.