Clojure 中的 agent

首页 / Clojure入门教程 / Clojure 中的 agent

如果代理失败,则返回在代理的异步操作期间抛出的异常,如果代理没有失败,则返回nil。

agent-error - 语法

(agent-error agentname)

参数      -  " agentname"是应该为其设置agent-error函数的代理。

返回值  -  如果代理失败,则返回在代理的异步操作过程中引发的异常。如果代理没有失败,则返回nil。

agent-error - 示例

以下程序显示了有关如何使用它的示例。

无涯教程网

(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)

在上面的程序中,我们通过增加错误的日期变量的值来强制导致异常发生。这将导致异常,并在" prinltn"语句的帮助下将其发送到屏幕。

上面的程序产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/clojure/clojure-agenterror.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

深入浅出区块链 -〔陈浩〕

微服务架构实战160讲 -〔杨波〕

代码精进之路 -〔范学雷〕

性能工程高手课 -〔庄振运〕

动态规划面试宝典 -〔卢誉声〕

爆款文案修炼手册 -〔乐剑峰〕

深入浅出分布式技术原理 -〔陈现麟〕

高并发系统实战课 -〔徐长龙〕

好记忆不如烂笔头。留下您的足迹吧 :)