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.
Exceptions
If there’s an unusual situation that you cannot or don’t want to react to, you can throw an exception. It’s a sort of failure that will stop your current flow of execution and if you don’t handle it eventually, it will terminate your program with an error. This is the most common way to deal with errors.
Swift/Objective-C interop
With Kotlin Multiplatform, to translate the exceptions to Swift/Objective-C errors properly, functions must be annotated with @Throws. Otherwise, the exceptions will be impossible to handle and will always crash the program.
@Throws(IllegalArgumentException::class)
fun bridgedToIOS(param1: Int) {
require(param1 >= 0)
}