Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Reflection on Failure

A Reflection on Failure

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?

Daniel James

June 09, 2013
Tweet

More Decks by Daniel James

Other Decks in Programming

Transcript

  1. sealed abstract class Try[+T] final case class Success[+T](value: T) extends

    Try[T] final case class Failure[+T](ex: Throwable) extends Try[T]
  2. Option Try def apply(x: T): Option[T] def apply(x: 㱺 T):

    Try[T] Option { mightBeNull(…) } Try { mightThrowEx(…) }
  3. Option Try def map[U](f: T 㱺 U): Try[U] def flatMap[U](f:

    T 㱺 Try[U]): Try[U] def foreach[U](f: T 㱺 U): Unit def filter(f: T 㱺 Boolean): Try[T]
  4. try { val res = try { callRemoteService(…) } catch

    { case _: Exception => try { callBackupService(…) } catch { case _: Exception => useLocalService(…) } } displayResult(res) } catch { case ex: ServiceException => … case ex: DisplayException => … }
  5. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => Success(f(x)) case _ => this } the functor laws
  6. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => Success(f(x)) case _ => this } the functor laws myTry map identity ≡ myTry 1
  7. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => Success(f(x)) case _ => this } the functor laws myTry map identity ≡ myTry 1 myTry map f map g ≡ myTry map (f andThen g) 2
  8. def flatMap[U](f: T 㱺 Try[U]): Try[U] = this match {

    case Success(x) => f(x) case _ => this } the monad laws
  9. def flatMap[U](f: T 㱺 Try[U]): Try[U] = this match {

    case Success(x) => f(x) case _ => this } the monad laws Success(x) flatMap f ≡ f(x) 1
  10. def flatMap[U](f: T 㱺 Try[U]): Try[U] = this match {

    case Success(x) => f(x) case _ => this } the monad laws Success(x) flatMap f ≡ f(x) 1 myTry flatMap (Success(_)) ≡ myTry 2
  11. def flatMap[U](f: T 㱺 Try[U]): Try[U] = this match {

    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
  12. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => case _ => this } Success(f(x))
  13. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => case _ => this } Success(f(x))
  14. def map[U](f: T 㱺 U): Try[U] = this match {

    case Success(x) => try Success(f(x)) catch { e: Exception => Failure(e) } case _ => this }
  15. the ‘monad’ laws 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 }
  16. the ‘monad’ laws Success(x) map f ≡ f(x) 1 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 }
  17. the ‘monad’ laws Success(x) map f ≡ f(x) 1 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 }
  18. the ‘monad’ laws Success(x) map f ≡ f(x) 1 myTry

    map (Success(_)) ≡ myTry 2 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 }
  19. the ‘monad’ laws Success(x) map f ≡ f(x) 1 myTry

    map (Success(_)) ≡ myTry 2 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 }
  20. the ‘monad’ laws Success(x) map f ≡ f(x) 1 myTry

    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 }
  21. the ‘monad’ laws Success(x) map f ≡ f(x) 1 myTry

    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 }
  22. the ‘monad’ laws Success(x) map f ≡ Try { f(x)

    } 1 myTry map identity ≡ myTry 2 myTry map f map g ≡ myTry map { x => Try { 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 }
  23. def map[B](f: A => B): Option[B] = if (isEmpty) None

    else Some(f(this.get)) <— null?