A Frayed Knot
A Frayed Knot

Reputation: 469

Scala - Why does my for loop not return?

I'm attempting to perform some retry logic with Scala. For the sake of providing a MRE, I have replaced the exception-throwing object with Y. When running X.main. I expect to see this print two failure messages, then one success message, but it appears that the return statement is effectively acting as a no-op, and I'm seeing 3 success messages.

I realize this isn't idiomatic Scala and there are plenty of other ways to accomplish this, but is there no way to return from within a for loop in Scala? If not, why is that?

object X {
  def main(args : Array[String]) : Unit = {

    val y = new Y()

    for ( i <- 1 to 5 ) {
      try {
        y.doSomething()
        return
      } catch {
        case e : Throwable =>
          if ( i == 5 ) {
            throw e
          }
      }
    }
  }
}

class Y {
  var counter = 0

  def doSomething() : Unit = {
    if ( counter < 2 ) {
      println("Tried to do something, but failed")
      counter += 1
      throw new RuntimeException
    }
    println("Did something")
  }
}

Upvotes: 0

Views: 117

Answers (1)

Dima
Dima

Reputation: 40500

return is even more weird than you thought, not just "non-idiomatic". It actually throws an exception. Your catch clause catches and swallows it. That's why it "acts as no-op".

Don't do this. Please.

But, if you insist, do, at least catch { case NonFatal(e) => instead as suggested in the comment - that would let through the exceptions thrown by return.

Upvotes: 4

Related Questions