How is try a value-producing expression in Kotlin, and how do optional semicolons and statement termination work?
answer
- try/catch returns a value; finally does not
- Result = last expr of try, or of the matching catch
- Semicolons optional — newline ends a statement
- Need ; only for two statements on one line
- enum body needs ; before member declarations
basics
~10 sKotlin's try/catch can return a value you assign to a variable. Semicolons are optional because newlines end statements; you only need them to put two statements on one line.
solid answer
~40 sLike `if` and `when`, `try`/`catch` is an **expression** in Kotlin: its value is the last expression of the `try` block, or, if an exception is caught, the last expression of the matching `catch` block. A `finally` block runs for cleanup but does **not** contribute the result value. This lets you write `val n = try { s.toInt() } catch (e: NumberFormatException) { 0 }`. Separately, **semicolons are optional**: Kotlin uses newlines as statement terminators, so you almost never write `;`. The only common case is separating multiple statements on a single line, or an `enum` body before its members' methods. This expression model (if/when/try) plus optional semicolons is what makes single-expression, statement-light Kotlin idiomatic.
code
kotlin · 7 linesval parsed: Int = try {
"42x".toInt()
} catch (e: NumberFormatException) {
0 // this becomes the value of the expression
} finally {
println("done") // runs, but does NOT supply the value
}go deeper
Knows semicolons are optional and that try/catch exists.
Explains try as an expression, where its value comes from, and that finally doesn't contribute it.
Reasons about newline-as-terminator effects on call-chain formatting and enum semicolon edge cases.
Connects expression-oriented try plus optional semicolons to a statement-light style and weighs it against readability/error-handling clarity.
## try as an expression In Kotlin, `try`/`catch` **produces a value**, so you can assign or return it directly: ```kotlin val number: Int = try { input.toInt() // value if no exception } catch (e: NumberFormatException) { -1 // value if this catch runs } ``` - The result is the **last expression** of the `try` block if it completes normally. - If an exception is thrown and caught, the result is the **last expression of the matching `catch`** block. - A **`finally`** block is for cleanup only; it runs regardless but **does not** provide the result value. ```kotlin val r = try { compute() } catch (e: Exception) { fallback() } finally { close() } // r comes from compute() or fallback(), never from finally ``` Used as a statement (value ignored), `try` behaves like the familiar form. ## Optional semicolons Kotlin treats **newlines** as statement terminators, so `;` is almost always unnecessary: ```kotlin val a = 1 val b = 2 // no semicolons needed ``` You need a semicolon only to: - Put **multiple statements on one line**: `val a = 1; val b = 2`. - Separate an `enum` constant list from member declarations in the same body: ```kotlin enum class Dir { N, S, E, W; // semicolon required before the function fun opposite() = N } ``` ## How line breaks interact with expressions Because newlines end statements, where you break a line matters. An operator that continues an expression should be placed so the line is clearly incomplete (e.g. trailing `+` or a leading `.` on the next line for call chains): ```kotlin val total = items .filter { it > 0 } .sum() // leading dot keeps the chain going ``` ## The big picture `if`, `when`, and `try` are all expressions, and `;` is optional. Together these let Kotlin favor returning values over mutating temporaries, enabling concise single-expression functions.
- Can a finally block change the value returned by a try expression?No. finally runs for side effects/cleanup but does not contribute the expression's value; the value comes from try or the matching catch.
- When is a semicolon actually required in Kotlin?To separate multiple statements on one line, and to separate enum constants from member declarations within the same enum body.
- Thinking finally supplies the try expression's value
- Adding semicolons everywhere out of Java habit
- Breaking a call chain so a line looks complete and changes meaning
- Not knowing try can be assigned to a val
- Forgetting the semicolon after enum constants before methods