Since agents run in a thread pool, the way that
exceptions are signaled and handled becomes an issue. Does the offending message function simply stop running? Does the agent stop? What happens to the Exception
instance?
Clojure has several mechanisms for dealing with errors in agent functions. We'll walk through them in this recipe.
The agent's error mode and its error handler determine how the agent handles errors. The error mode can be set when the agent is created or with the function set-error-mode!
, and the error handler is set with set-error-handler!
.
The default error mode is :fail
. If we don't specify an error mode when we create an agent, this is what it gets. With this error mode, when one of the agent's message functions throws an exception, the agent stops processing any more messages and stores the exception. We can retrieve the exception with agent-error
, and we can start processing again with restart-agent
.
user=...