# Concise way to manage resource with exception in kotlin

**URL:** <https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636>\
**Category:** Language Design\
**Created:** [December 1, 2017, 12:33pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636 "2017-12-01T12:33:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 1, 2017, 12:33pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/1 "2017-12-01T12:33:38Z")

</div>

Hi, new to kotlin here. I just stumble with managing resource with exception.

When doing IO operation, `IOException` is just normal and expected (internet connection closed suddenly, disk full, etc), so it must caught and properly handled.

In Java we can use `try with resource` to make a method _never throw_ `IOException` and resource will _properly closed_ when the method return.

Simplified example in Java

```
public ParseResult parse(File file) {
    try (InputStream stream = new FileInputStream(file)) {
        // do something with stream, might throw IOExcpetion here
        return ParseResult.success(...);
    } catch(IOException e) {
        return ParseResult.failed(e);
    }
}

```

Same functionality in kotlin that I can come up looks more verbose, and somehow uglier because deeper level of `{}`

```
fun parse(file: File): ParseResult {
    return try {
        file.inputStream().use {
            // do something with stream, might throw IOExcpetion here
            ParseResult.success(...)
        }
    } catch(e: IOException) {
        ParseResult.failed(e)
    }
}

```

Are there more concise and idiomatic way to manage resource that might throw exception in kotlin? Am I missing something?

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [December 1, 2017, 12:39pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/2 "2017-12-01T12:39:58Z")

</div>

On the JVM there is [`use`](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html).

---

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 1, 2017, 1:00pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/3 "2017-12-01T13:00:59Z")

</div>

> On the JVM there is use

Yes, I already know `use` (right in the example posted above). But nesting `use` inside `try - catch` looks more verbose than Java.

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [December 1, 2017, 1:14pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/4 "2017-12-01T13:14:00Z")

</div>

Doh, sorry. I just glanced over the examples.

But in that case, there is no more concise syntax. That should not be a problem though, because it is already very concise (the only things that are “redundant” are the 2 keywords and some braces), and it is usually not used in a lot of places.

If you do use it a lot, you can write an extension function that contains the `use` invocation and `try`-`catch` statement.

---

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 1, 2017, 1:36pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/5 "2017-12-01T13:36:33Z")

</div>

> Doh, sorry. I just glanced over the examples.

It is okay 😉

> If you do use it a lot, you can write an extension function that contains the use invocation and try-catch statement.

Hmm… thanks for your suggestion. I will consider that option.

I thought something similar to `try-with-resource` already exists in the standard library.

_edit_

I have tried using extension function and ended up creating multiple extension function for `File`, `URL`, etc, which is IMHO not flexible enough (or maybe I just don’t know proper way to use it)

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [December 1, 2017, 1:42pm UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/6 "2017-12-01T13:42:38Z")

</div>

Succes/Fail is a functional `Try`

You can optionally define:

```
fun Closeable.tryUse(block: () -> R) = Try{ use{ block() } }

```

`Try` here: [funKTionale/Try.kt at master · MarioAriasC/funKTionale · GitHub](https://github.com/MarioAriasC/funKTionale/blob/master/funktionale-try/src/main/kotlin/org/funktionale/tries/Try.kt)

---

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 2, 2017, 12:45am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/7 "2017-12-02T00:45:55Z")

</div>

Thanks for pointing out functional Try.

Functional try don’t have functionality to automatically release / `close()` the resource, and must be handled manually.

> ```
> val Closeable.tryUse(block: () -> R) = Try{ use{ block() } }
> 
> ```

The problem is, acquiring `Closeable` itself might throw `IOException`.

---

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 4, 2017, 3:42am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/8 "2017-12-04T03:42:51Z")

</div>

I am experimenting with functional try idea above, end up with idea of `LazyTry`

```
class LazyTry<C: Closeable>(private val create: () -> C) {

    fun <T> use(transform: (C) -> T): Result<T> {
        return try {
            create().use { transform(it) }.let { Result.success(it) }
        } catch (e: IOException) {
            Result.failed(e)
        }
    }
}

```

It will lazily open the resource, at the time `LazyTry::use()` method invoked.

And using it will look something like this

```
LazyTry { file.inputStream() }.use {
    it.reader().readText()
}.onSuccess {
    // consume the result when success
    println(it)
}.onFailed {
    // handle the exception when failed
    logger.info("failed to read file", it)
}

```

Resource will be closed automatically and exception will be caught, so it mimic Java try with resource.

What do you think? Is it acceptable Kotlin style?

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 4, 2017, 6:45am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/9 "2017-12-04T06:45:46Z")

</div>

I don’t like it, because you seem to be unable to write something like

val foo = LazyTry{…}

Also it would be better if this was a function call rather than a constructor call (lower case) because you don’t really want to be aware of the internal creation of the Result object.

Also needs a finally block.

I feel this really needs a language feature and all the attempts here are crude workarounds.

---

<div class="post-metadata">

**Author:** ![devb](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@devb](https://discuss.kotlinlang.org/u/devb)\
**Post date:** [December 4, 2017, 9:51am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/10 "2017-12-04T09:51:07Z")

</div>

Thanks for your response

> … you seem to be unable to write something like
> 
> ```
> val foo = LazyTry{…}
> 
> ```

Sorry, I am not quite sure I know what you mean

> ```
> val openLater = LazyTry { file.inputStream() }
> 
> ```

Will create an object `openLater` without actually opening the file, and can be `use()`-ed multiple time.

> ```
> openLater.use { read(it) }
> openLater.use { doSomethingWith(it) }
> openLater.use { doOtherThingWith(it) }
> 
> ```

Each time `use` invoked, the `InputStream` will be created and closed when the `use` block end

> Also it would be better if this was a function call rather than a constructor call (lower case)

Yes, good point. Maybe top level function like this will do

```
fun <C: Closeable> tryWith(create: () -> C) = LazyTry(create)

```

Then, my original question can be rewritten in kotlin like this

```
fun parse(file: File) : Result {
    return tryWith {
        file.inputStream()
    }.use {
        doSomethingWith(it)
    }
}

```

> Also needs a finally block.

As far as I understand, the purpose of `finally` is to clean up resources, which is already handled by `Closeable.use` extension method

> I feel this really needs a language feature and all the attempts here are crude workarounds.

Totally agree with you!  
~~Kotlin is supposed to be “Better Java” (or so I heard). This problem already solved since Java 7!~~. Too bad it is not implemented yet.

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 5, 2017, 8:08am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/11 "2017-12-05T08:08:48Z")

</div>

I would like to do something like

```auto
readLock.lock()
val jsonObject = try(val inputStream = InputStream(...) ) {
 Json.read(inputStream) 
} 
catch(e: IOException) {
 Logger.log(e)
 throw e
} 
finally {
 readLock.unlock()
} 

```

As for the finally block I often found that you need to do something in addition to closing the Closable object, like release a ReadWriteLock as in the example or log a message or whatever. Don’t have a specific example right now but I found it very handy to be able to use try-with-resources with finally in Java.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [December 5, 2017, 8:57am UTC](https://discuss.kotlinlang.org/t/concise-way-to-manage-resource-with-exception-in-kotlin/5636/12 "2017-12-05T08:57:39Z")

</div>

Use `withLock`

> **[withLock - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/java.util.concurrent.locks.-lock/with-lock.html)**
