All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitlab.mvysny.konsumexml.Utils.kt Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy