Kotlin:
Handling errors
How to:
Kotlin provides try
, catch
, finally
, and throw
to manage errors. Here’s how you use them:
fun main() {
val numerator = 10
val denominator = 0
try {
val result = numerator / denominator
println("Result: $result")
} catch (e: ArithmeticException) {
println("Can't divide by zero, buddy.")
} finally {
println("This happens no matter what.")
}
}
Output:
Can't divide by zero, buddy.
This happens no matter what.
If something goes wrong in the try
block, execution zips to the catch
. It catches the specific error thrown (ArithmeticException
in this case). The finally
block runs after—no matter the outcome.
Deep Dive
The try-catch
block has been a thing since early programming days—it’s like a safety net. Kotlin also offers throw
for manually tossing an exception into the ring, and there’s finally
for code that’s gotta run—clean-up work, often.
Alternatives include the Result
type and Kotlin’s try
as an expression.
val result: Result<Int> = try {
Result.success(numerator / denominator)
} catch (e: ArithmeticException) {
Result.failure(e)
}
This approach returns a Result
object—you get either a success or a failure without the drama of an unhandled exception.
Implementation in Kotlin is neat because you can use try
like an expression, meaning it returns a value. Choices like these make error handling in Kotlin pretty versatile. It’s about choosing the right tool for the job, just like you would in a workshop.
See Also
- Kotlin docs on Exceptions: Kotlin Exception Handling
- Kotlin
Result
type docs: Kotlin Result - Effective Java, 3rd Edition, by Joshua Bloch—great insights on exceptions, though it’s Java-specific.