Complete the code to catch an exception of type ArithmeticException.
try { val result = 10 / 0 } catch (e: [1]) { println("Cannot divide by zero") }
The code catches an ArithmeticException which occurs when dividing by zero.
Complete the code to catch a NullPointerException.
val text: String? = null try { println(text!!.length) } catch (e: [1]) { println("Null value encountered") }
The code throws a NullPointerException when trying to access length of a null string.
Fix the error in the catch block to handle IndexOutOfBoundsException.
val list = listOf(1, 2, 3) try { println(list[5]) } catch (e: [1]) { println("Index is out of range") }
Accessing an invalid index throws IndexOutOfBoundsException.
Fill both blanks to catch ArithmeticException and NullPointerException separately.
try { val number = 10 / 0 val text: String? = null println(text!!.length) } catch (e: [1]) { println("Arithmetic error occurred") } catch (e: [2]) { println("Null pointer error occurred") }
The first catch handles ArithmeticException and the second handles NullPointerException.
Fill all three blanks to catch ArithmeticException, NullPointerException, and IndexOutOfBoundsException in separate catch blocks.
try { val result = 10 / 0 val text: String? = null println(text!!.length) val list = listOf(1, 2, 3) println(list[5]) } catch (e: [1]) { println("Arithmetic error") } catch (e: [2]) { println("Null pointer error") } catch (e: [3]) { println("Index out of bounds error") }
Each catch block handles a specific exception: ArithmeticException, NullPointerException, and IndexOutOfBoundsException.