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 Exception Handling with examples

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

Exceptions are unwanted issues that can occur at runtime of the program and terminate your program abruptly. Exception handling is a process, using which we can prevent the program from such exceptions that can break our code.

There are two types of exceptions:
1. Checked exceptions that are declared as part of method signature and are checked at the compile time, for example IOException
2. Unchecked exceptions do not need to be added as part of method signature and they are checked at the runtime, for example NullPointerException.

Note: In Kotlin all exceptions are unchecked.

Handling of exception in Kotlin is same as Java. We use try, catch and finally block to handle the exceptions in the kotlin code.

Kotlin Exception handling example

In the following example we are dividing a number with 0 (zero) which should throw ArithmeticException. Since this code is in try block, the corresponding catch block will execute.

In this case the ArithmeticException occurred so the catch block of ArithmeticException executed and “Arithmetic Exception” is printed in the output.

When an exception occurs, it ignores everything after that point and the control instantly jumps to the catch block if any. The finally block is always executed whether exception occurs or not.

fun main(args: Array<String>) {
    try {

        var num = 10/0
        println("BeginnersBook.com")
        println(num)


    } catch (e: ArithmeticException) {
        println("Arithmetic Exception")
    } catch (e: Exception) {
        println(e)
    } finally {
        println("It will print in any case.")
    }
}

Output:
Kotlin exception handling

What happens if we don’t handle exception?

Lets say if we don’t handle the exception in the above example then the program would terminate abruptly.
Here we didn’t handle exception so the program terminated with an error.

fun main(args: Array<String>) {

        var num = 10/0
        println("BeginnersBook.com")
        println(num)
}

Output:
Kotlin exception handling error when we don't handle them

How to throw an exception in Kotlin

We can also throw an exception using throw keyword. In the following example we are throwing an exception using throw keyword. The statement before the exception got executed but the statement after the exception didn’t execute because the control transferred to the catch block.

fun main(args: Array<String>) {
    try{
        println("Before exception")
        throw Exception("Something went wrong here")
        println("After exception")
    }
    catch(e: Exception){
        println(e)

    }
    finally{
        println("You can't ignore me")
    }
}

Output:
Kotlin throw keyword

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Try Catch with example
  2. Kotlin Higher order function with example
  3. Kotlin Multiple Catch Blocks with example
  4. Kotlin Keywords, Soft Keywords and Identifiers
  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