Exception Handling
Exception Handling
What are Exceptions?
Exceptions are runtime errors that disrupt the normal flow of a program. Kotlin provides structured exception handling using try, catch, finally, and throw.
Video Explanation

Basic try-catch
fun main() {
try {
val result = 10 / 0
println(result)
} catch (e: ArithmeticException) {
println("Error: ${e.message}")
}
}
// Output: Error: / by zero
try-catch-finally
finally always runs, regardless of whether an exception occurred:
fun main() {
try {
val nums = arrayOf(1, 2, 3)
println(nums[10]) // Index out of bounds
} catch (e: ArrayIndexOutOfBoundsException) {
println("Caught: ${e.message}")
} finally {
println("This always executes")
}
}