Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created January 16, 2012 21:41
Show Gist options
  • Select an option

  • Save missingfaktor/1623178 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/1623178 to your computer and use it in GitHub Desktop.
import util.control.ControlThrowable
case object RestartException extends ControlThrowable
def restart: Nothing = throw RestartException
def attempt[A](f: => A): Attempt[A] = new Attempt(f)
class Attempt[A](f: => A) {
def fallback(catcher: PartialFunction[Throwable, A]): A = {
var stop = true
var result = null.asInstanceOf[A]
do {
try {
result = f
stop = true
} catch {
case t: Throwable if catcher isDefinedAt t => {
try {
result = catcher(t)
stop = true
} catch {
case RestartException => stop = false
}
}
case t: Throwable => throw t
}
} while(!stop)
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment