Clojure - agent-error



Returns the exception thrown during an asynchronous action of the agent, if the agent fails. Returns nil if the agent does not fail.

Syntax

Following is the syntax.

(agent-error agentname)

Parameters − ‘agentname’ is the agent for which the agent-error function should be set to.

Return Value − Returns the exception thrown during an asynchronous action of the agent if the agent fails. Returns nil if the agent does not fail.

Example

An example on how this is used is shown in the following program.

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def my-date (agent(java.util.Date.)))
   (send my-date + 100)
   (await-for 100 my-date)
   (println (agent-error my-date)))
(Example)

In the above program we are forcefully causing an exception to occur by incrementing the value of a date variable which is wrong. This will cause an exception and with the help of the ‘prinltn’ statement, will be sent to the screen.

Output

The above program produces the following output.

Exception in thread "main" java.lang.RuntimeException: Agent is failed, needs
start, compiling:(C:\Users\Administrator\demonew\src\demonew\main.clj:9:1)
   at clojure.lang.Compiler.load(Compiler.java:7239)
   at clojure.lang.Compiler.loadFile(Compiler.java:7165)
   at clojure.main$load_script.invoke(main.clj:275)
   at clojure.main$init_opt.invoke(main.clj:280)
   at clojure.main$initialize.invoke(main.clj:308)
   at clojure.main$null_opt.invoke(main.clj:343)
   at clojure.main$main.doInvoke(main.clj:421)
   at clojure.lang.RestFn.invoke(RestFn.java:421)
   at clojure.lang.Var.invoke(Var.java:383)
   at clojure.lang.AFn.applyToHelper(AFn.java:156)
   at clojure.lang.Var.applyTo(Var.java:700)
   at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Agent is failed, needs restart
   at clojure.lang.Util.runtimeException(Util.java:225)
   at clojure.lang.Agent.dispatch(Agent.java:238)
   at clojure.core$send_via.doInvoke(core.clj:1995)
   at clojure.lang.RestFn.invoke(RestFn.java:445)
   at clojure.lang.AFn.applyToHelper(AFn.java:160)
   at clojure.lang.RestFn.applyTo(RestFn.java:132)
   at clojure.core$apply.invoke(core.clj:636)
   at clojure.core$send.doInvoke(core.clj:2006)
   at clojure.lang.RestFn.invoke(RestFn.java:425)
   at clojure.core$await_for.doInvoke(core.clj:3177)
   at clojure.lang.RestFn.invoke(RestFn.java:423)
   at clojure.examples.example$Example.invoke(main.clj:6)
   at clojure.examples.example$eval12.invoke(main.clj:9)
   at clojure.lang.Compiler.eval(Compiler.java:6782)
   at clojure.lang.Compiler.load(Compiler.java:7227)
   ... 11 more
clojure_agents
Advertisements