Kotlin is advertised as an extremely expressive language. However, it's one of the characteristics of the language that is not obvious in the beginning, especially if you are used to other languages such as Java or JavaScript. In order to present the language style more clearly, in this recipe, we are going to discover how to work with the try…catch declaration in a Kotlin way, by treating it as an expression.
Expressive try...catch declarations
Getting ready
Let's consider the following Java code:
int value;
try {
result = parseInt(input);
} catch (NumberFormatException e) {
} finally {
result = 0;
}
It declares an int result variable. Next, it tries to parse the string value to the integer with the Integer...