0

Here is my question! When I use several future tasks in a for expression, I'm confused with the result of exception recover.

object ForTest {    
  def ft1(n: Int): Future[Int] = Future(n*2)
  def ft2(n: Int): Future[Int] = Future(60/n)
  def main(args : Array[String]) : Unit = {
    val n = 0
    val res =
      for {
        a1 <- ft1(n)
        a2 <- ft2(n)
      } yield (a1,a2)    
    res.map{ x =>
      println(s"x=$x")
    }.recover{
      case t: Throwable =>
        println(s"exception ${t.getStackTrace}")
    }    
  }
}

Above is my code. Sometimes it print out the exception, sometimes not, I'm totally confused. Can anyone help me?

0 Answers0