com.gitlab.mvysny.konsumexml.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konsume-xml Show documentation
Show all versions of konsume-xml Show documentation
Konsume-XML: A simple functional XML parser with no annotations
package com.gitlab.mvysny.konsumexml
import java.io.Closeable
/**
* Tries to run given block on a [Closeable]. If the block fails, this closable is closed; if the block succeeds,
* this closeable is not closed since it's expected that the closeable will be used further.
*/
inline fun T.andTry(block: (T) -> R): R = try {
block(this)
} catch (e: Exception) {
try {
close()
} catch (ce: Exception) {
// we don't depend on slf4j so we can't use that;
// we need to run on older Androids and therefore we can't use Throwable.addSuppressed()
// JUL is not supported properly on Androids
// the safest thing is to fall back and call printStackTrace()
ce.printStackTrace()
}
throw e
}