Book Image

Clojure Data Analysis Cookbook - Second Edition

By : Eric Richard Rochester
Book Image

Clojure Data Analysis Cookbook - Second Edition

By: Eric Richard Rochester

Overview of this book

Table of Contents (19 chapters)
Clojure Data Analysis Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Recovering from errors in agents


Since agents run in a thread pool, the way 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 to deal with errors in agent functions. We'll walk through them in this recipe.

How to do it…

The agent's error mode and error handler determine how it 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!.

Failing on errors

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 it again with restart-agent:

user=> (def agent-99...