Panic in Kotlin
In Kotlin, the concept of panic is not directly present. Instead, we use exceptions for unexpected errors. Here’s how we can implement similar behavior:
Running this program will cause it to throw an exception, print an error message and stack trace, and exit with a non-zero status.
When the first exception in main
is thrown, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the first exception.
Note that unlike some languages which use exceptions for handling of many errors, in Kotlin it is idiomatic to use nullable types and the Result type for error handling where possible. However, for truly unexpected errors, exceptions are still used.