# Try-with-resources or use()

**URL:** <https://discuss.kotlinlang.org/t/try-with-resources-or-use/460>\
**Category:** Uncategorized\
**Created:** [September 23, 2014, 10:37am UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460 "2014-09-23T10:37:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [September 23, 2014, 10:37am UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/1 "2014-09-23T10:37:46Z")

</div>

try-with-resources and the Kotlin subsitute use() were mentioned in an [older discussion](http://confluence.jetbrains.com/display/Kotlin/Exceptions?focusedCommentId=51187556#comment-51187556). The link to the use() functions leads to nowhere. So what is the Kotlin solution for try-with-resources today?

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [September 23, 2014, 12:12pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/2 "2014-09-23T12:12:59Z")

</div>

Provided link doesn't seem to be related.

use() function is now extension, e.g.:

myStream.use {  
&nbsp;&nbsp;val line = readLine()  
}

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [September 23, 2014, 12:17pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/3 "2014-09-23T12:17:28Z")

</div>

Thanks. Would be nice to see it in the [docs](http://kotlinlang.org/docs/reference/exceptions.html).

PS: I have corrected the link.

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [September 23, 2014, 1:36pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/4 "2014-09-23T13:36:03Z")

</div>

Easy to fix:

[https://github.com/JetBrains/kotlin-web-site/pull/87](https://github.com/JetBrains/kotlin-web-site/pull/87)

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [September 23, 2014, 1:37pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/5 "2014-09-23T13:37:43Z")

</div>

> use() function is now extension, e.g.:
> 
> myStream.use {  
> &nbsp;&nbsp;val line = readLine()  
> }

This doesn’t appear to do what you think it does. If I control-click readLine here I’m taken to the global method that reads from stdin.

---

<div class="post-metadata">

**Author:** ![kocka](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kocka](https://discuss.kotlinlang.org/u/kocka)\
**Post date:** [September 23, 2014, 6:00pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/6 "2014-09-23T18:00:22Z")

</div>

We had a [discussion](https://discuss.kotlinlang.org/thread/455328?tstart=0) about this question two months ago, Andrey said there are **no plans** to support the try-with-resources.

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [September 24, 2014, 9:18am UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/7 "2014-09-24T09:18:24Z")

</div>

Andrey said about try-with-resources as a dedicated language feature. use() standard library function have been available for a long time.

---

<div class="post-metadata">

**Author:** ![mario.macias.upc](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@mario.macias.upc](https://discuss.kotlinlang.org/u/mario.macias.upc)\
**Post date:** [March 28, 2016, 10:25am UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/8 "2016-03-28T10:25:11Z")

</div>

What about?  
package info.macias.kotlin

```
inline fun <T:AutoCloseable,R> trywr(closeable: T, block: (T) -> R): R {
    try {
        return block(closeable);
    } finally {
        closeable.close()
    }
}

```

Example of use:

```
import info.macias.kotlin.trywr
fun countEvents(sc: EventSearchCriteria?): Long {
    return trywr(connection.prepareStatement("SELECT COUNT(*) FROM event")) {
        var rs = it.executeQuery()
        rs.next()
        rs.getLong(1)
    }
}

```

Gist: [https://gist.github.com/mariomac/e81477965fbe03a9dd8a](https://gist.github.com/mariomac/e81477965fbe03a9dd8a)

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [March 28, 2016, 12:40pm UTC](https://discuss.kotlinlang.org/t/try-with-resources-or-use/460/9 "2016-03-28T12:40:16Z")

</div>

In version 1.0, Kotlin does not support standard library multitargeting, so the standard library can only refer to classes that exist in Java 6. That’s the reason why we don’t include a version of use() that references AutoCloseable in the standard library. You’re, of course, welcome to use your variant of the function in your own code.

Support for standard library multitargeting is on the roadmap for Kotlin 1.1. For AutoCloseable support specifically, you’re welcome to watch and vote for [https://youtrack.jetbrains.com/issue/KT-5899](https://youtrack.jetbrains.com/issue/KT-5899)
