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} |
In this example the otherwise block gets executed only if system throws ClosedChannelExeption, but I could easily imagine other control abstractions hiding different exception types.
No comments:
Post a Comment