Created
April 8, 2016 16:05
-
-
Save khalidr/a2f20cd7e99ed0384ff762e74adeb976 to your computer and use it in GitHub Desktop.
ErrorHandling using AllCatch
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
//structural typing | |
def closingOrElse[T,CLOSEABLE <: {def close() : Unit}](resource: CLOSEABLE)(block: CLOSEABLE => T)(handler: Throwable => T) : T = { | |
allCatch.andFinally(ignoring(classOf[Throwable]){ resource.close() }){ | |
allCatch.withApply{ e =>handler(e) } | |
{ | |
block(resource) | |
} | |
} | |
} | |
//Closable | |
def withCloseable[T <: Closeable, R](t: T)(f: T ⇒ R): R = { | |
allCatch.andFinally { t.close() } apply { f(t) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment