jkbbwr
07/30/2017, 11:37 PMinline fun <T : Closeable?, R> T.tryUse(block: (T) -> R, exceptionHandler: (Exception) -> Unit): R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
closed = true
try {
this?.close()
} catch (closeException: Exception) {
}
exceptionHandler(e)
throw e
} finally {
if (!closed) {
this?.close()
}
}
}
fun main(args: Array<String>) {
val x = X()
x.tryUse({
println("doin something")
}, {
println("it died :(")
})
}