When we run program code, we deal with expressions and statements all the time. It's very important to understand the difference between these. Let's look at the following code:
1 + 1
This code is an expression because it contains variables, operators, and returns a single result. If a standalone element of program code represents an action, it is a statement:
println("Hello")
In the context of this section, the main point for us is that an expression returns something. In Kotlin, the try { ... } catch { ... } finally { ... } block is an expression and we can write something like this:
fun loadValue(): Int = throw Exception()
fun main(args: Array<String>) {
println(try { loadValue() } catch (exception: Exception) { 4 })
}
Under the hood, this code works in the same way as if we were to write it in Java. To check this...