加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

斯卡拉 – 作为阿卡的一名演员的回应捕捉异常

发布时间:2020-12-16 19:23:33 所属栏目:安全 来源:网络整理
导读:以下函数调用一个actor: def read () = { val system = ActorSystem(Constant.actorSystem) val manageData = system.actorOf(Props[ManageData],name = "theactor") val num = -1 implicit val timeout = Timeout(60 seconds) val future = manageData ? n
以下函数调用一个actor:

def read ()  = {

  val system = ActorSystem(Constant.actorSystem)
  val manageData = system.actorOf(Props[ManageData],name = "theactor")

  val num = -1
  implicit val timeout = Timeout(60 seconds)
  val future = manageData ? num
  val result = Await.result(future,timeout.duration)
}

在manageData中,子进程抛出异常:

throw new Exception("Negative number")

如何在read()中捕获它?

解决方法

这是一个古老的问题,但我不得不处理它.

如果异常是我不期待的,我通常会在演员主管上对待它:

override val supervisorStrategy =
  OneForOneStrategy(maxNrOfRetries = 10,withinTimeRange = 1 minute) {
    case _: ArithmeticException      => {
      log.error("n# ArithmeticException -> Resumen")
      Resume
    }
    // other exceptions
  }
}

对于其他类型的错误,我倾向于回复错误并在以下情况下处理它:

case class IError(code:Int,msg:Option[String] = None)

sender ! IError(401,msg= Some("Unauthorized"))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读