RosettaCodeData/Task/Detect-division-by-zero/Kotlin/detect-division-by-zero.kts
2024-10-16 18:07:41 -07:00

20 lines
389 B
Kotlin

// version 1.1
fun divideByZero(x: Int, y:Int): Boolean =
try {
x / y
false
} catch(e: ArithmeticException) {
true
}
fun main(args: Array<String>) {
val x = 1
val y = 0
if (divideByZero(x, y)) {
println("Attempted to divide by zero")
} else {
@Suppress("DIVISION_BY_ZERO")
println("$x / $y = ${x / y}")
}
}