ps0604
ps0604

Reputation: 1071

How to catch Future.failed in Scala?

Why does this print "Success" instead of "Failure"? How to catch Future.failed?

object HelloScala extends App{

      val f = future
      f.map { result => println("Success")
      }.recover { case e =>
        println(e.getMessage)
      }


      def future = Future {
          Future.failed(new Throwable("Failure"))
      }

      Thread.sleep(10000)

}

Upvotes: 0

Views: 176

Answers (1)

jwvh
jwvh

Reputation: 51271

Look at the type of f.

val f: Future[Future[_]] = future

The inner Future fails but the outer Future completes with Success.

Upvotes: 2

Related Questions