commonMain.Cleanup.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konapi-jvm Show documentation
Show all versions of konapi-jvm Show documentation
Kotlin Native for Raspberry Pi
The newest version!
package ch.softappeal.konapi
public inline fun tryFinally(tryBlock: () -> R, finallyBlock: () -> Unit): R {
var tryException: Exception? = null
return try {
tryBlock()
} catch (e: Exception) {
tryException = e
throw tryException
} finally {
try {
finallyBlock()
} catch (finallyException: Exception) {
if (tryException == null) throw finallyException
tryException.addSuppressed(finallyException)
}
}
}
public interface Closeable {
public fun close()
}
public inline fun C.use(block: (closeable: C) -> R): R = tryFinally({
block(this)
}) {
close()
}
public inline fun tryCatch(tryBlock: () -> R, catchBlock: () -> Unit): R = try {
tryBlock()
} catch (tryException: Exception) {
try {
catchBlock()
} catch (catchException: Exception) {
tryException.addSuppressed(catchException)
}
throw tryException
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy