Skip to content

Instantly share code, notes, and snippets.

@piotrga
Created December 25, 2011 21:36
Show Gist options
  • Save piotrga/1519778 to your computer and use it in GitHub Desktop.
Save piotrga/1519778 to your computer and use it in GitHub Desktop.
Try-Catch abstraction
connected {
doSomethingDangerous()
} otherwise {
println("Error! Got disconnected.")
}
def doSomethingDangerous() = {/*...*/}
def connected(body: => Unit): Result =
try {
body; Ok
} catch {
case e: ClosedChannelException => Failed
}
trait Result { def otherwise(onError: => Unit): Unit}
case object Ok extends Result { def otherwise(onError: => Unit) = () /* do nothing */}
case object Failed extends Result { def otherwise(onError: => Unit) = onError}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment