Try recover get vs Try match

Viewed 1820

In order to catch exception, I can use Try recover get:

Try(op) recover {
  case e: FooException => log; default
} get

Edit: or fold as pointed out in the comment for Scala 2.12+

or I can use Try match:

Try(op) match {
  case Success(v) => v
  case Failure(e: FooException) => log; default
  case Failure(e)=> throw e
}

What is the difference between these two? Which one is more idiomatic? What is the reasoning? Is there any performance implication?

1 Answers

The match version is better. There are three possible outcomes (value, default, exception) and the match version makes this clear whereas the recover version is more obscure. The recover version also has a bare get which is usually a bad sign. match is also likely to perform better, though the cost of either version will be small compared to the work in Try(op).

Related