Created
January 16, 2012 21:41
-
-
Save missingfaktor/1623178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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