Tag: exception处理的

Scala期货和`andThen`exception传播

我在scala.concurrent.Future模块中阅读了Scala 2.11.8和andThen函数的文档,它说如下: def andThen[U](pf: PartialFunction[Try[T], U]) (implicit executor: ExecutionContext): Future[T] 将副作用函数应用于此未来的结果,并返回具有此未来结果的新未来。 此方法允许强制执行回调以指定顺序执行。 请注意,如果链接的andThen回调之一抛出exception,则该exception不会传播到后续的andThen回调。 相反,后续的andThen回调将被赋予此未来的原始值。 我不确定它是什么意思是exception不会被传播, andThen也没有提供示例。 例如,如果我做这样的事情: Future.successful { throw new RuntimeException(“test”) } andThen { case _ => println(“test”) } 在Scala REPL中我得到: java.lang.RuntimeException: test … 32 elided 所以exception被传播了。 有人可以提供一个有意义的例子,这究竟意味着什么,以及是否安全使用andThen代码,我抛出exception,我想从中恢复。 谢谢。