Sobes.tech
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 try block and handle exceptions in catch blocks.
  • finally: a block that always executes after try and catch, used for resource cleanup.
  • throw: manually throw exceptions.
  • Creating custom exceptions: you can create classes that inherit from Throwable or 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")