How to catch many exceptions at the same time in Kotlin?

Exception handling is an important feature of any programming language. It restricts our programs from generating incorrect output at runtime. Kotlin does not support throwing multiple exceptions at the same time, however we can implement this functionality using some other functions provided by the Kotlin library.

Example – Throwing Multiple Exceptions

In this example, we will try to generate an arithmetic exception at runtime and we will try to catch the same in the catch block along with other different exception checks.

fun main(args: Array<String>) {
   val item=0
   var result=0
   try {
      result=item/0 // Exception: Division by Zero
   } catch (ex: Exception) {
      when(ex) {
         // inlcude multiple exceptions here
         is IllegalAccessException, is IndexOutOfBoundsException -> {
         is ArithmeticException ->{
         else-> throw ex


Here, we are throwing multiple exceptions at the catch block. It will produce the following output −

java.lang.ArithmeticException: / by zero

Updated on: 16-Mar-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started