【发布时间】:2019-05-20 10:42:09
【问题描述】:
此代码按预期工作:
it("should propagate exceptions") {
intercept[RuntimeException] {
val future = Future { Thread.sleep(10); sys.error("whoops"); 22 }
Await.result(future, Duration.Inf)
}.getMessage should equal ("whoops")
}
但这不是:
it("should propagate errors") {
intercept[StackOverflowError] {
val future = Future { Thread.sleep(10); throw new StackOverflowError("dang"); 22 }
Await.result(future, Duration.Inf)
}.getMessage should equal ("dang")
}
第二个测试中的未来永远不会回来。为什么Error 子类(而不是Exception 子类)不会终止我的未来?我应该如何处理Errors?
编辑:这可能与Why does Scala Try not catching java.lang.StackOverflowError? 相关,但不完全相同。我没有在这里使用Try。核心问题是Future 永远不会返回;我无法从中捕获任何错误,因为它只是挂起。
【问题讨论】:
标签: scala exception error-handling future