我试图使用scala.concurrent.Future
重现此问题How do I get hold of exceptions thrown in a Scala Future?,我希望可以吞下该异常,但似乎并未发生。有什么解释吗?
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
def runWithTimeout[T](timeoutMs: Long)(f: => Future[T]) : T = {
Await.result(f, timeoutMs.millis)
}
runWithTimeout(50) { Future { "result" } }
runWithTimeout(50) { Future { throw new Exception("deliberate") } }
答案 0 :(得分:20)
期货做不吞下例外(至少不是通常意义上的,这意味着“捕获和丢弃”)。在将来抛出异常时(在map
/ flatMap
的主体中或使用Future.apply
时),异常确实不会传播到当前线程中,但会保留在未来,成为未来的结果。事实上,未来的最终结果是Try[T]
,因此它将使用T
(a Success[T]
),或类型{{1}的正确结果完成{1}}(Throwable
)。
另一个关键点是,当你调用Failure[T]
时,你基本上要求通过等待未来的最终结果来阻止当前线程,无论是正确的结果还是异常 。在异常的情况下,它将在调用Await.result
的位置重新抛出。
如果您不希望重新引发异常,则可以使用Await.result
代替。无论是正常完成还是通过例外,它都会等待未来的完成并返回未来。然后,您可以通过Await.ready