The Try can be also used as Try expression in Kotlin. The Try expression returns the last expression of try block or last expression of catch. The finally block working doesn’t get affected in Try expression. To understand Try expression is Kotlin please check out the following example program.
Page Contents
Example Program without Exception:
fun main(args: Array<String>){
val str = div(10,2)
println("Ans "+str)
}
fun div(num1: Int, num2: Int): Int{
return try {
num1/num2
} catch (e: ArithmeticException) {
0
}
}
Output:
Ans 5
Example Program with Exception:
fun main(args: Array<String>){
val str = div(10,0)
println("Ans "+str)
}
fun div(num1: Int, num2: Int): Int{
return try {
num1/num2
} catch (e: ArithmeticException) {
0
}
}
Output:
Ans 0

Parvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.