Complete the code to catch an exception in Kotlin.
try { val result = 10 / 0 } catch (e: [1]) { println("Error occurred") }
The Exception class is used to catch errors in Kotlin.
Complete the code to throw an exception when a condition is false.
fun checkAge(age: Int) {
if (age < 18) {
throw [1]("Age must be 18 or older")
}
}IllegalArgumentException is used to indicate that a method has been passed an illegal or inappropriate argument.
Fix the error in the code to properly handle a nullable value with try-catch.
fun parseNumber(str: String?): Int {
return try {
str?.toInt() ?: throw [1]("Null string")
} catch (e: NumberFormatException) {
0
}
}NullPointerException is appropriate to throw when a null value is unexpected.
Fill both blanks to create a try-catch block that logs the error message.
try { val data = fetchData() } catch (e: [1]) { Log.e("Error", e.[2]) }
Catch Exception to handle errors, and use message to get the error description.
Fill all three blanks to define a custom exception class and throw it.
class [1](message: String) : [2](message) fun test() { throw [3]("Custom error") }
Define a custom exception MyException that inherits from Exception and throw it.
