Every program should grapple with failure. A happy path is only a part of the story. There are many ways to handle errors. The essential toolkit showcases throwing and catching exceptions, passing results, leveraging optionals, aborting, and asserting.

Optionals

Sometimes we only care about values and lack thereof, and are not interested in errors. Then we can represent a failure or a missing value by null. This only works for values that do not assign a special meaning to null already, of course.

Note: Optionals can simplify error handling, but can lead to problems with sparse data.

Optionals

fun eval(input: String): Int? = input.toIntOrNull()

[--]