Kotlin program of using try-catch as an expression
fun DivideOperation(x: Int, y: Int) : Any {
return try {
x/y
}
catch(exp:Exception){
println(exp)
"Divide by zero exception occured"
}
}
// Main function
fun main(args: Array<String>) {
// Call DivideOperation() functions
// to use try-catch as an exception
var res1 = DivideOperation(10, 3)
println(res1)
var res2 = DivideOperation(10, 0)
println(res2)
}
Output:
3
java.lang.ArithmeticException: / by zero
Divide by zero exception occured
