Mandroid
Mandroid

Reputation: 7494

Best practices on error handling in Scala using Try

I have handled exception as follows:

def calculate(input: Option[Double]): Try[String] =
  Try {
    input match {
      case (Some(value)) => value.toString
      case (None) => throw new IllegalArgumentException("No value found")
    }
}

And in client code:

val result = calculate(....)

result match {
    case Success(i) => println(i)
    case Failure(s) => throw s // or log(s) to log the issue and continue
}

Is it good enough practice or much better can be done for clean and elegant code base?

Upvotes: 1

Views: 1715

Answers (1)

Ivan Kurchenko
Ivan Kurchenko

Reputation: 4063

Try usually used to cover parts which might throw an error, like in cases if you are using some Java libs, which can throw an exception. But, if you would like to return possible error and force client to handle it, Either[A, B] is much better option, at least because you can specify more precise error type for Left[A] and safely to pattern match over your's A type, instead of do possibly incorrect pattern matching against some Throwable, like you would do for Failure(t).

So, in your case possible solution would look like:

sealed trait CalculationError
case class Error1(cause: String) extends CalculationError

def calculate(input: Option[Double]): Either[CalculationError, String] =
    input match {
      case (Some(value)) => Right(value.toString)
      case (None) => Left(Error1("No value found"))
    }
}

val result = calculate(....)

result match {
    case Right(i) => println(i)
    case Left(Error1(s)) => println(s)
}

This is safer approach, because you can later add another type of error , say case class Error2(cause: String) extends CalculationError and on client pattern matching code part, compile will show a warn message that you missed handling of new error: Match is not exhaustive. In case of Failure(t) compile won't be able suggest such warning, so it's easier to make mistake on error handling side.

Hope this helps!

Upvotes: 8

Related Questions