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 Nested Try-Catch Block with example

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

When a try catch block is present inside another try catch block then it is called nested try catch block. If any exception occurs in the inner try catch block which is not handled in the inner catch blocks then the catch blocks of the outer try catch blocks are checked for that exception.

Syntax of nested try catch block

try{
     //code
     try{
           //code
     }
     catch
     {
          //handler
     }
}
catch
{
          //handler
}

Nested Try-Catch Block example

In the following example there is an exception in the inner try block but the occurred exception (ArithmeticException) is not handled in the inner catch blocks so the outer catch blocks are checked for this exception, since outer catch block is handling this exception, the code inside outer catch block is executed for ArithmeticException.

There can be more than one try catch blocks in a try block, also there can be a try catch block inside the inner try block as well. The only thing to take note here is that if the exception is not handled in the child try catch block, then the handlers of parent try catch blocks are checked for the occurred exception.

fun main(args: Array<String>) {
    try {
        val num = 100 / 5
        println(num)
        try {
            val num2 = 100 / 0
            println(num2)
        }
        catch(e: NumberFormatException){
            println("Number Format Exception")
        }
    }
    catch(e: ArithmeticException){
        println("Arithmetic Exception")
    }
}

Output:
Kotlin Nested Try Catch example

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Try Catch with example
  2. Kotlin Interfaces with examples
  3. Kotlin Exception Handling with examples
  4. Kotlin Nested and Inner Class with examples
  5. Kotlin Higher order function with example

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