5

According to the source of Closable.use, if an error occurs, an exception will be thrown.

public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
    var exception: Throwable? = null
    try {
        return block(this)
    } catch (e: Throwable) {
        exception = e
        throw e
    } finally {
        when {
            apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception)
            this == null -> {}
            exception == null -> close()
            else ->
                try {
                    close()
                } catch (closeException: Throwable) {
                    // cause.addSuppressed(closeException) // ignored here
                }
        }
    }

In most examples of Closable.use, try-catch is not used as shown below. Why isn't error handling needed? Is it safe?

    BufferedReader(FileReader("test.file")).use { return it.readLine() }
10
  • 1
    This Q&A is for Java but the same concepts should apply in Kotlin: When to catch the Exception vs When to throw the Exceptions?.
    – Slaw
    Jun 2, 2020 at 16:00
  • @Tenfour04 shouldn't the try/catch be enclosing the use function?
    – goedi
    Jun 2, 2020 at 16:12
  • @Tenfour04 It's use which can throw the exception that I believe the OP is worried about.
    – Slaw
    Jun 2, 2020 at 16:12
  • Putting try/catch inside use is like doing a traditional try/catch/finally. Putting it outside causes the exceptions to be handled after the object is closed. If the act of closing the object can itself throw an exception, then you need to catch that outside use. This behavior is similar to Java's try-with-resources.
    – Tenfour04
    Jun 2, 2020 at 16:51
  • 1
    Interesting that use works differently than Java's try-with-resources when it comes to stacked exceptions from both the virtual try and finally blocks. use suppresses the exception from close, but try-with-resources suppresses the exception from try.
    – Tenfour04
    Jun 2, 2020 at 17:20

2 Answers 2

5

This line

 BufferedReader(FileReader("test.file")).use { return it.readLine() }

is not safe. Reading and closing the reader can both throw IOExceptions, which are not RuntimeExceptions (caused by programming errors). That means leaving them uncaught exposes your app to crashing from things outside your control.

Since Kotlin doesn't have checked exceptions, the compiler won't warn you about this. To do this safely, you need to wrap it in try/catch. And if you want to handle read errors differently than close errors, you either need to have inner and outer try/catch statements:

try { 
    BufferedReader(FileReader("test.file")).use { 
        try {
            return it.readLine()
        catch (e: IOException) {
            println("Failed to read line")
        }
    }
} catch (e: IOException) {
    println("Failed to close reader")
}

or wrap the whole thing and extract any suppressed exceptions, but then its cumbersome to distinguish between them:

try {
    BufferedReader(FileReader("test.file")).use { return it.readLine() }
} catch (e: IOException) {
    val throwables = listOf(e, *e.suppressed)
    for (throwable in throwables)
        println(throwable.message)
}

But in practice, you're probably not going to react differently to various IOExceptions, so you can just put the one try/catch outside.

4

We see from Kotlin documentation what is the purpose of the use function:

Executes the given block function on this resource and then closes it down correctly whether an exception is thrown or not.

This function closes the resource properly if the block function completed successfully or threw an exception. It is your responsibility to handle the result of the block function.

If an exception was thrown and there is a way to handle it and proceed with code execution, use a try/catch. If there is nothing to do about it and control should be passed to the caller, it is not necessary to use a try/catch.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.