Created
December 25, 2011 21:36
-
-
Save piotrga/1519778 to your computer and use it in GitHub Desktop.
Try-Catch abstraction
This file contains 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
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