How do I get hold of exceptions thrown in a Scala Future?

后端 未结 6 1135
我在风中等你
我在风中等你 2020-12-06 10:30

I\'ve been working up my answer to Is there a standard Scala function for running a block with a timeout?, and have run into a problem if an exception is thrown in a Future.

6条回答
  •  忘掉有多难
    2020-12-06 10:32

    Disclaimer: I work for Typesafe

    Or.... you could use Akka and it would give you what you want without you having to go through hoops for it.

    val f: Future[Int] = actor !!! message
    

    Then

        f.get 
    

    Will throw the exception that happened in the actor

        f.await.exception 
    

    will give you an Option[Throwable]

提交回复
热议问题