【发布时间】:2016-06-08 11:45:53
【问题描述】:
为什么会出现异常
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
object TestExceptionHandling {
def main(args: Array[String]): Unit = {
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()(defaultActorSystem)
Source(List(1, 2, 3)).map { i =>
if (i == 2) {
throw new RuntimeException("Please, don't swallow me!")
} else {
i
}
}.runForeach { i =>
println(s"Received $i")
}
}
}
默默地忽略?我可以看到在打印Received 1 后流停止了,但没有记录任何内容。请注意,问题一般不在于日志记录配置,因为如果我在 application.conf 文件中设置 akka.log-config-on-start = on 会看到很多输出。
【问题讨论】:
-
您正在丢弃异常,因为您忽略了
runForeach的返回值。 -
@ViktorKlang 感谢您指出这一点,我刚刚更新了我的答案!
标签: scala exception-handling akka-stream