RuntimeException } recoverWith { case e: IllegalArgumentException => Future.successful(0) } Try { ??? throw new RuntimeException } match { case Success(result) => result case Failure(e) => println(s"failure :${e.getMessage}") } Either { ??? Left("Error") } match { case Right(success) => success case Left(e) => println(s"failure: $e") }
String): Either[String, Int] = for { n <- value result <- if(n < 0) Left(e) else Right(n * 2) } yield result def program(value: Future[Int], e: => String)(implicit ec: ExecutionContext): Future[Int] = for { n <- value result <- if(n < 0) Future.failed(new RuntimeException(e)) else Future.successful(n*2) } yield result def program(value: Try[Int], e: => String): Try[Int] = for { n <- value result <- if(n < 0) Failure(new RuntimeException(e)) else Success(n*2) } yield result