Database Reference
In-Depth Information
user=> @agent-99
0
user=> (restart-agent agent-99 0)
0
Continuing on errors
The other option for an error mode is :continue . With this error mode, when an agent
throws an exception, the exception and the message that caused it are silently swallowed.
The agent continues processing as if nothing has happened:
user=> (def agent-99 (agent 0 :error-mode :continue))
#'user/agent-99
user=> (send agent-99 #(/ 100 %))
#<Agent@f1b6fa6: 0>
user=> (agent-error agent-99)
nil
user=> @agent-99
0
Using a custom error handler
If the agent's error handler is set, that is used instead of the error mode. The error handler
is just a function that is called with the agent and the exception that was thrown. In this
example, the new error handler will simply print the exception:
user=> (def agent-99 (agent 0 :error-handler #(prn %2)))
#'user/agent-99
user=> (send agent-99 #(/ 100 %))
#<ArithmeticExceptionjava.lang.ArithmeticException: Divide by zero>
#<Agent@2da13aa9: 0>
user=> (agent-error agent-99)
nil
user=> @agent-99
0
Once an error has been caught using a custom error handler, the agent is free to continue
processing. If we send agent-99 a new message, it will process it as if nothing has happened.
There's more...
The canonical source of information about Clojure's agent system and how it handles
errors is the Agents and Asynchronous Actions page in the Clojure documentation at
http://clojure.org/agents .
 
Search WWH ::




Custom Search