0

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")
  }
}

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, use NonFatal to avoid catching the ControlThrowable that returns throws. See scala-lang.org/api/2.12.4/scala/util/control/NonFatal$.html
Thanks! And in regards to your "don't do this" comment - I wasn't actually planning on it. I was more so just wanting to understand how return behaved under the sheets. I rewrote this using tail recursion before even posting this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.