New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
See the Tutorial List

Kotlin Multiple Catch blocks

In this tutorial we will learn how and why to use multiple catch blocks with try block to handle exceptions in Kotlin. A try block can have multiple catch blocks. Multiple catch blocks are used to catch different type of exceptions.

The syntax of multiple catch block is:

try {
    // Code which may throw multiple exceptions
}catch (exception: ExceptionType1){
    // Handling exception
}catch (exception: ExceptionType2){
    // Handling exception
}catch (exception: ExceptionType3){
    // Handling exception
}

As you can see above, we have specified different exception types for each catch block. Multiple catch blocks are used to handle different types of exceptions differently, for example, print different message for user in case of different exceptions.

Kotlin Multiple catch block - Example

Let us first see an example in which we will catch multiple exceptions. We will take two numbers as input from user and divide them:

fun main() {
    try {
        val a: Int = Integer.parseInt(readLine())
        val b: Int = Integer.parseInt(readLine())
        println("The Division of $a and $b is: ${a/b}")
    }catch (exception: NumberFormatException){
        println("Caught NumberFormatException: Numbers entered are invalid!!")
    }catch (exception: ArithmeticException){
        println("Caught ArithmeticException: Divided by zero!!")
    }catch (exception: Exception){
        println("Caught Exception!!")
    }

}

Here we have caught NumberFormatException, ArithmeticException and general Exception. For different inputs, output is:

  • a = 10, b = 5


    The Division of 10 and 5 is: 2

  • a = 10, b = 0


    Caught ArithmeticException: Divided by zero!!

  • a = 10, b = "Hello"


    Caught NumberFormatException: Numbers entered are invalid!!

As you can see in the code examples above, we have handled 3 exceptions, using 3 different catch blocks. We have used to specific exception classes, namely, NumberFormatException and ArithmeticException which will handle specific use cases.

Then we have the third catch block with Exception class which is the parent class for many exception classes. The order of exceptions in multiple catch blocks also matters.

  • The order of catch block must be specific to general. The specific exception must be caught first(like we the NumberFormatException and ArithmeticException first) and then the generalized exception should be caught at last.
    If we place the Exception class first, all others exceptions will be caught by this catch block only.

  • Another important point to remember is that only one catch block will be executed. Only one exception can be occur at a time and the catch block for that particular exception is executed.

Summary

In this tutorial we learned how we can use multiple catch block in Kotlin to handle multiple different exception types. In the next tutorial we will discuss about nested catch block in Kotlin.