SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package errorhandling
  2.  
  3. object MakeIntEither extends App {
  4.  
  5.   def makeInt(s: String): Either[String, Int] = {
  6.     try {
  7.       Right(s.trim.toInt)
  8.     } catch {
  9.       case e: Exception => Left(e.toString)
  10.     }
  11.   }
  12.  
  13.  
  14.   makeInt("1")
  15.  
  16.   makeInt("11f") match {
  17.     case Left(s) => println("Error message: " + s)
  18.     case Right(i) => println("Desired answer " + i)
  19.   }
  20. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top