BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Multiple Catch Blocks with example

Last Updated: March 7, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

A try block can have multiple catch blocks. When we are not sure what all exceptions can occur inside the try block then it is always a good idea to have multiple catch blocks for the potential exceptions and in the last catch block have the parent exception class to handle the remaining exceptions that are not specified by catch blocks.

Kotlin multiple catch blocks example

In the following example we have multiple catch blocks but when an exception occurs it looks for the handler for that particular exception.

The exception occurred here is Arithmetic exception, however the first two catch blocks didn’t handle the Arithmetic exception thats why the third catch block’s code executed. Third block is handling all the exceptions because it is using the Exception class which is a parent of all the exception classes.

fun main(args: Array<String>) {
    try{
        var num = 10/0
        println(num)
    }
    catch(e: NumberFormatException){
        println("Number format exception")
    }
    catch(e: ArrayIndexOutOfBoundsException){
        println("Array index is out of range")
    }
    catch(e: Exception){
        println("Some Exception occurred")
    }

    println("Out of try catch block")
}

Output:
Kotlin multiple catch blocks

Another example of multiple catch blocks

Here is another example of multiple catch blocks, here ArrayIndexOutOfBoundsException occurred, since there is a handler (catch block) present for this exception, the code inside the handler is executed.

fun main(args: Array<String>) {
    try{
        val a = IntArray(5)
        a[10] = 99
    }
    catch(e: ArithmeticException){
        println("ArithmeticException occurred")
    }
    catch(e: NumberFormatException){
        println("Number format exception")
    }
    catch(e: ArrayIndexOutOfBoundsException){
        println("Array index is out of range")
    }
    catch(e: Exception){
        println("Some error occurred")
    }

    println("Out of try catch block")
}

Output:
Kotlin multiple catch blocks example

Why it is a good idea to have parent Exception class in the last catch block

Lets take the same example that we have above but in this code, we have made a small change. Here we have the handler (catch block) of parent Exception class in the first place.

In the code ArrayIndexOutOfBoundsException occurred and we have the handler for this particular exception but since we have the general Exception class in the first place and it handles all the exceptions so it got executed instead of the catch block that handles ArrayIndexOutOfBoundsException.

The catch blocks are checked in the sequential manner so the first catch block is executed, in fact in case of any exception the first catch will execute which is a bad programming practice because we want a specific message rather than a generalized message. So the solution is to have this default handler at the last place like we did in the above example.

fun main(args: Array<String>) {
    try{
        val a = IntArray(5)
        a[10] = 99
    }
    catch(e: Exception){
        println("Some error occurred")
    }
    catch(e: ArithmeticException){
        println("ArithmeticException occurred")
    }
    catch(e: NumberFormatException){
        println("Number format exception")
    }
    catch(e: ArrayIndexOutOfBoundsException){
        println("Array index is out of range")
    }
    println("Out of try catch block")
}

Output:
Kotlin general Exception handling in multiple catch blocks

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Try Catch with example
  2. Kotlin Exception Handling with examples
  3. Kotlin Higher order function with example
  4. Kotlin Constructors with examples
  5. Kotlin Abstract Class with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap