summaryrefslogtreecommitdiff
path: root/src/library/scala/util/control/Breaks.scala
blob: b3eaad23aeb6097378d268649c039c75650a592d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package scala.util.control

object Breaks {
  private class BreakException extends RuntimeException
  private val breakException = new BreakException
  private class ContinueException extends RuntimeException
  private val continueException = new ContinueException

  /** A block from which one can exit with a `break' and which can be resumed with a `continue'. */
  def breakable(op: => Unit) {
    try {
      op
    } catch {
      case ex: BreakException =>
      case ex: ContinueException => breakable(op)
    }
  }

  /* Break from closest enclosing breakable block */
  def break { throw breakException }

  /* Continue with start of closest enclosing breakable block */
  def continue { throw continueException }
}