# Remove Curly-braces in try, catch, finnaly blocks

**URL:** <https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557>\
**Category:** Language Design\
**Created:** [February 8, 2018, 10:19pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557 "2018-02-08T22:19:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![megamzero](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@megamzero](https://discuss.kotlinlang.org/u/megamzero)\
**Post date:** [February 8, 2018, 10:19pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/1 "2018-02-08T22:19:20Z")

</div>

A simple try catch block can looks like this:

```
fun readMessage(reader: () -> String): String =
        try {
            reader()
        } catch (e: Exception) {
            e.message ?: "error"
        }

```

Why not doing it like this for single instruction :

```
fun readMessage(reader: () -> String): String = 
        try reader() catch (e: Exception) e.message ?: "error" 

```

Like if/else blocks does

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 9, 2018, 12:05am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/2 "2018-02-09T00:05:57Z")

</div>

Really like this idea. I think it would be even more useful when assigning values.

```nohighlight
val a = try evaluate() catch (e: Exception) someErrorHandling(e)

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 9, 2018, 6:32am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/3 "2018-02-09T06:32:35Z")

</div>

Curly braces removal for various cases was discussed multiple times here (I am personally completely opposed to the idea).

As for your example, it usually leads to performance problems to catch errors from single operation. Catch and re throw exceptions is very slow on JVM, so usually there should be only one catch block on the top level and it should be visible.

---

<div class="post-metadata">

**Author:** ![ahmadalsharif.dev](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@ahmadalsharif.dev](https://discuss.kotlinlang.org/u/ahmadalsharif.dev)\
**Post date:** [February 9, 2018, 6:57am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/4 "2018-02-09T06:57:57Z")

</div>

This depends on the developer’s needs I guess, it would still look like a nice option to have lying around

---

<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:** [February 9, 2018, 8:00am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/5 "2018-02-09T08:00:47Z")

</div>

Is the following lines valid?

```
val a = try evaluate() catch (e: IOException) someIOErrorHandling(e) catch (e: Exception) someErrorHandling(e)

val a = try try evaluate() catch (e: IOException) someIOErrorHandling(e) catch (e: Exception) someErrorHandling(e)

```

---

<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:** [February 9, 2018, 8:36am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/6 "2018-02-09T08:36:35Z")

</div>

Your own example is a great illustration of why this is not a great idea. Since `try` is an expression in Kotlin, your example has two possible interpretations:

```
   (try { reader() } catch (e: Exception) { e.message }) ?: "error" 

```

or

```
    try { reader() } catch (e: Exception) { e.message ?: "error" }

```

Making code harder to understand for the sake of saving a couple of characters in a construct which isn’t commonly used as a one-line statement is hardly a worthwhile tradeoff.

---

<div class="post-metadata">

**Author:** ![megamzero](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@megamzero](https://discuss.kotlinlang.org/u/megamzero)\
**Post date:** [February 9, 2018, 8:57am UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/7 "2018-02-09T08:57:56Z")

</div>

Never think the statement could be interpreted like this… And I could not see any kind of solution to it.  
You just blast the idea. Ok then.

---

<div class="post-metadata">

**Author:** ![Antoni.Mysliborski](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@Antoni.Mysliborski](https://discuss.kotlinlang.org/u/Antoni.Mysliborski)\
**Post date:** [February 9, 2018, 3:31pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/8 "2018-02-09T15:31:36Z")

</div>

I would say default parameters (like in lambdas) would help a lot:  
try { reader() } catch { it.message ?: “error” }  
(or maybe at least catch (e) { e.message ?: “Error” }

---

<div class="post-metadata">

**Author:** ![megamzero](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@megamzero](https://discuss.kotlinlang.org/u/megamzero)\
**Post date:** [February 9, 2018, 4:41pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/9 "2018-02-09T16:41:06Z")

</div>

Maybe not `catch (e) { ... }` but something like this :

```kotlin
try { evaluate() } 
catch (EvaluateException) { named -> evalutationErrorHandling(named) } // explicit name
catch (IOException) { ioErrorHandling(it) } // `it` is IOException
catch { defaultErrorHandling(it) } // `it` is Exception by default

```

---

<div class="post-metadata">

**Author:** ![Antoni.Mysliborski](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@Antoni.Mysliborski](https://discuss.kotlinlang.org/u/Antoni.Mysliborski)\
**Post date:** [March 27, 2018, 2:26pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/10 "2018-03-27T14:26:00Z")

</div>

This would be backwards incompatible so low chance of passing - my proposition is just omitting type optionally (as we do in many other cases) but doesn’t change the syntax for existing code nor introduces something completely not compatible with current way of handling exception

---

<div class="post-metadata">

**Author:** ![JavierSC](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JavierSC](https://discuss.kotlinlang.org/u/JavierSC)\
**Post date:** [March 22, 2019, 1:26pm UTC](https://discuss.kotlinlang.org/t/remove-curly-braces-in-try-catch-finnaly-blocks/6557/11 "2019-03-22T13:26:28Z")

</div>

I like the OP idea. I think it should have the same behavior as if/else expression when it is a single statement.

Should be great too if this behavior is added to another functions like `let`.
