Junior — Middle
Milliseid toiminguid saab Kotlinis teha eranditega töötades?
sobes.tech AI
Vastus AI-lt
In Kotlin, when working with exceptions, you can perform the following operations:
- try-catch: wrap code that may throw an exception in a
tryblock and handle exceptions incatchblocks. - finally: a block that always executes after
tryandcatch, used for resource cleanup. - throw: manually throw exceptions.
- Creating custom exceptions: you can create classes that inherit from
Throwableor its subclasses.
Example:
try {
val result = 10 / divisor
} catch (e: ArithmeticException) {
println("Division by zero")
} finally {
println("This block will execute in any case")
}
throw IllegalArgumentException("Invalid argument")