Exception Handling (Try, Catch, Finally) in Kotlin

 package com.example.my_kotlin_practice


fun main(args: Array<String>) {
try {
var a = "abc".toInt()
}
catch(e: NumberFormatException) {
println("String can't be converted to number because of ${e.message}")
}
// catch(e: Exception) {
// println("String can't be converted to number because of ${e.message}")
// }
finally {
println("This will execute everytime")
}
println("Processing done")
// isEven(3)
}
//fun isEven(num: Int) {
// if(num%2 != 0) {
// throw IllegalArgumentException("Number is not even")
// }
//}

OUTPUT :

String can't be converted to number because of For input string: "abc" This will execute everytime Processing done Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin