How does exception handling in Java and Scala compare? What are the appealing aspects of handling exceptions as values using scala.util.Try (new in Scala 2.10)? Is scala.util.Try a monad… and does it matter?
case Success(x) => f(x) case _ => this } the monad laws Success(x) flatMap f ≡ f(x) 1 myTry flatMap (Success(_)) ≡ myTry 2 myTry flatMap f flatMap g ≡ myTry flatMap { x => f(x) flatMap g } 3
map (Success(_)) ≡ myTry 2 myTry map f map g ≡ myTry map { x => f(x) map g } 3 def map[U](f: T 㱺 ex[U]): Try[U] = this match { case Success(x) => try Success(f(x)) catch { e: Exception => Failure(e) } case _ => this }
map (Success(_)) ≡ myTry 2 myTry map f map g ≡ myTry map { x => f(x) map g } 3 def map[U](f: T 㱺 ex[U]): Try[U] = this match { case Success(x) => try Success(f(x)) catch { e: Exception => Failure(e) } case _ => this }