Computer scienceProgramming languagesKotlinControl flowExceptionsException types

Catching supertype

Bring order

Report a typo

The code provided below is not working as expected due to the order of the catch blocks. Your task is to fix the code so that each exception is properly caught and handled.

Sample Input 1:

Hello

Sample Output 1:

You did not enter a valid integer!

Sample Input 2:

0

Sample Output 2:

You cannot divide by zero!
Write a program in Kotlin
fun main() {
val input = readln()
try {
println(100 / input.toInt())
} catch (e: Exception) {
println("An unknown error has occurred!")
} catch (e: NumberFormatException) {
println("You did not enter a valid integer!")
} catch (e: ArithmeticException) {
println("You cannot divide by zero!")
}
}
___

Create a free account to access the full topic