Exception Handling in Kotlin with Examples
Last Updated :
16 Aug, 2020
Exceptions are the error which comes at the runtime and disrupts your flow of execution of the program. Basically exception is the unwanted event that occurs at runtime. The method by which we can handle this kind of error is called Exception Handling.
Types of Exceptions
- Checked Exception: Exceptions that occur at the compile-time, or we can say that checked at the compile time is called Checked Exception. Example — IOException, Some file related Exception, etc
- Unchecked Exception: Exceptions that occur at the runtime is called Unchecked Exception. Example — OutofBound exception.
Note: In Kotlin we have only unchecked Exceptions which only figure out at the runtime.
How to Handle Exceptions?
We have some keywords which help us to handle Exceptions.
- Try // This will try to find the exception
- Throw // If exception found then it will throw the exception
- Catch // After throwing it will catch the exception and execute their body.
We have one more keyword called Finally, It will always execute either we got exception or not. In this, we have some important codes that always need to execute.
Note: If the program exit by exitProcess(Int) or abort(), then the finally will not be executed.
How to Use try-catch and finally?
Kotlin
try
{
// your code which
// may throw an exception
}
catch(ex : ExceptionName)
{
// Exception handle code
}
finally
{
// this will execute every time
// either we found exception or not
}
Example 1: Divide By Zero
Kotlin
fun main()
{
try
{
// calling the function
divide(10, 0)
}
catch (ex : Exception)
{
// or you can also write your
// own handle here
println(ex.message)
}
}
// function which may give exception
fun divide(a : Int, b : Int)
{
if (b == 0)
throw Exception("Divide by zero")
// even if you didn't write
// this throw it will work fine.
println("Division is :" + a / b)
}
Output:
Divide by zero
Example 2: Let's try the same code with try-catch and finally.
Kotlin
fun main()
{
// first try block
try
{
// didn't throw any exception
divide(20, 10)
}
catch (ex : Exception)
{
// or you can also write your
// own handle here
println(ex.message)
}
finally
{
println("I'm executed")
}
// 2nd try block
try
{
// throw an exception
divide(10, 0)
}
catch (ex : Exception)
{
// or you can also write your
// own handle here
println(ex.message)
}
finally
{
println("I'm executed")
}
}
fun divide(a : Int, b : Int)
{
if (b == 0)
throw Exception("Divide by zero")
println("Division is :" + a / b)
}
Output:
Division is : 2
I'm executed
Divide by zero
I'm executed
Note that In the above example, finally is executed in both cases either exception occurs or not.
Similar Reads
Exception Handling in Kotlin Coroutines Coroutines are a new way for us to do asynchronous programming in Kotlin in Android. When developing a production-ready app, we want to ensure that all exceptions are handled correctly so that users have a pleasant experience while using our app. In this article, we will discuss how to properly hand
7 min read
Exception handling in Objective-C Exception handling is an essential aspect of Objective-C programming, enabling developers to manage unforeseen errors effectively. Objective-C provides a robust set of tools and methodologies to handle exceptions, ensuring the stability and reliability of applications. Let's delve deeper into the nu
5 min read
How to Throw a Custom Exception in Kotlin? In addition to the built-in Exception Classes, you can create your own type of Exception that reflects your own cause of the exception. And you will appreciate the use of Custom Exception when you have multiple catch blocks for your try expression and can differentiate the custom exception from regu
2 min read
Kotlin Exception Handling | try, catch, throw and finally Exception handling is an important part of programming that helps us manage errors in our code without crashing the entire application. In this article, we will learn about exception handling in Kotlin, how to use try, catch, throw, and finally blocks, and understand different types of exceptions.Ko
5 min read
use Keyword in Kotlin with Example Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read